using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Reflection;using System.IO;namespace ExcelTest{public partial class frmMain : Form{public frmMain(){InitializeComponent();}string[] ExcelColumTitle ={ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };int[] ExcelColumWidth ={10,15,15,12,12,12,20 };//各列宽度private void btnOk_Click(object sender, EventArgs e){this.Enabled = false;Excel.Application excelKccx = new Excel.Application();//创建excel对象excelKccx.Workbooks.Add(true);//创建excel工作薄int row = 2;//把数据表的各个信息输入到excel表中for (int i = 0; i < dataGridView1.Columns.Count; i++)//取字段名{excelKccx.Cells[1, i + 1] = dataGridView1.Columns[i].Name.ToString();}for (int i = 0; i < dataGridView1.Rows.Count; i++)//取记录值{for (int j = 0; j < dataGridView1.Columns.Count; j++){excelKccx.Cells[row, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();}row++;}for (int i = 0; i < dataGridView1.Columns.Count; i++){excelKccx.get_Range(ExcelColumTitle[i]+(i+1), Type.Missing).ColumnWidth =ExcelColumWidth[i] ; //宽度设置}for (int i = 0; i <= dataGridView1.Rows.Count; i++){excelKccx.get_Range("A" + (i + 1), Type.Missing).RowHeight = 14.25; //高度设置}excelKccx.Visible = true;//使excel可见*/}private void frmMain_Load(object sender, EventArgs e){List<Exceltabl> list = new List<Exceltabl>();string[] tempArray = Directory.GetFileSystemEntries(@"new\");for (int i = 0; i < tempArray.Length; i++){string tempLine = "";string tempAll = "";string tempA = "";//编号string tempB = "";//所属人string tempC = "";//概览string tempD = "";//CPU型号string tempE = "";//内存型号string tempF = "";//硬盘型号string tempG = "此处省略...";//详细string tempFile = tempArray[i];FileStream fs = new FileStream(tempFile, FileMode.Open);StreamReader sr = new StreamReader(fs, Encoding.Default);// tempAll = sr.ReadToEnd();tempA = string.Format("{0:00}", Path.GetFileNameWithoutExtension(tempFile));tempB = "待指定";StringBuilder sb = new StringBuilder();int index=0;while (true){if (index == 3)break;tempLine=sr.ReadLine();if (!string.IsNullOrEmpty(tempLine)){index = 0;int a = 0;int b = 0;a=tempLine.IndexOf("[");b=tempLine.IndexOf("]");if (b > a){string temp = tempLine.Substring(a, b + 1 - a);if (temp.Trim() == "[ 概览]"){for (int j = 0; j < 10; j++){sb.AppendLine(sr.ReadLine());}tempC = sb.ToString();sb = new StringBuilder();}else if (temp.Trim() == "[ 处理器]"){string tempStr = sr.ReadLine();while (true){tempStr = sr.ReadLine();if (string.IsNullOrEmpty(tempStr) || tempStr == " ") break;elsesb.AppendLine(tempStr);}tempD = sb.ToString();sb = new StringBuilder();}else if (temp.Trim() == "[ 硬盘]"){string tempStr = sr.ReadLine();while (true){tempStr = sr.ReadLine();if (string.IsNullOrEmpty(tempStr) || tempStr == " ") break;elsesb.AppendLine(tempStr);}tempF = sb.ToString();sb = new StringBuilder();}else if (temp.Trim() == "[ 内存]"){string tempStr = sr.ReadLine();while (true){tempStr = sr.ReadLine();if (string.IsNullOrEmpty(tempStr) || tempStr==" ") break;elsesb.AppendLine(tempStr);}tempE = sb.ToString();sb = new StringBuilder();}}}else{index++;}}sr.Close();fs.Close();Exceltabl tb = new Exceltabl();tb.编号= tempA;tb.所属人= tempB;tb.概览= tempC;tb.CPU型号= tempD;tb.内存型号= tempE;tb.硬盘型号= tempF;tb.详情= tempG;list.Add(tb);}dataGridView1.DataSource = list;}private void frmMain_FormClosing(object sender, FormClosingEventArgs e) {Application.Exit();}}class Exceltabl {private string a;public string 编号{get { return a; }set { a = value; }}private string b;public string 所属人{get { return b; }set { b = value; }}private string g;public string 概览{get { return g; }set { g = value; }}private string c;public string CPU型号{get { return c; }set { c = value; }}private string d;public string 内存型号{get { return d; }set { d = value; }}private string e;public string 硬盘型号{get { return e; }set { e = value; }}private string f;public string 详情{get { return f; }set { f = value; }}}}。