C#导出Excel文件

private void btnExls_Click(object sender, EventArgs e) { saveFD.Filter = "Excel文件(*.xls)|*.xls"; saveFD.FileName = DateTime.Now.Date.ToShortDateString() + "log.xls"; if (saveFD.ShowDialog().ToString() == "OK") { Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.ApplicationClass(); Microsoft.Office.Interop.Excel.Range range=null; app.Visible = false; Workbook wBook = app.Workbooks.Add(true); Worksheet wSheet = wBook.Worksheets[1] as Worksheet; string[] strs = txtVlog.Text.Split(new string[] { "/n" }, StringSplitOptions.RemoveEmptyEntries); range = wSheet.get_Range(wSheet.Cells[1, 1], wSheet.Cells[1, 3]); range.Font.Bold = true; ((Microsoft.Office.Interop.Excel.Range)wSheet.Columns["B",Type.Missing]).ColumnWidth = 100; ((Microsoft.Office.Interop.Excel.Range)wSheet.Columns["C", Type.Missing]).ColumnWidth = 20; wSheet.Cells[1, 1] = "NAME"; wSheet.Cells[1, 2] = "REFER"; wSheet.Cells[1, 3] = "STATIC Scanning"; if (strs.Length > 0) { for(int i=0;i<strs.Length;i++) { string[] ss = strs[i].Split(new string[] { "/t" }, StringSplitOptions.RemoveEmptyEntries); wSheet.Cells[i+2,1] = ss[0]; wSheet.Cells[i + 2, 2] = ss[1]; range = wSheet.get_Range("C"+(i+2), Missing.Value); if (ss[2].Trim()=="X") { range.Font.ColorIndex = 3; } else { range.Font.ColorIndex = 4; } wSheet.Cells[i + 2, 3] = ss[2]; } } app.DisplayAlerts = false; app.AlertBeforeOverwriting = false; //保存工作簿 wBook.Save(); //保存excel文件 app.Save(saveFD.FileName); app.SaveWorkspace(saveFD.FileName); app.Quit(); app = null; } } 

你可能感兴趣的:(工作,String,object,C#,Excel,null)