object missing = System.Reflection.Missing.Value;
String FileDir = Environment.CurrentDirectory;
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
app.Application.Workbooks.Add(true);
Microsoft.Office.Interop.Excel.Workbook book = (Microsoft.Office.Interop.Excel.Workbook)app.ActiveWorkbook;
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.ActiveSheet;
Microsoft.Office.Interop.Excel.Sheets sheets = book.Sheets;
Microsoft.Office.Interop.Excel._Worksheet worksheet = (Microsoft.Office.Interop.Excel._Worksheet)sheets.get_Item(1);
sheet.Cells[2, 1] = "序号";
sheet.Cells[2, 2] = "路径";
sheet.Cells[2, 3] = "文件名";
sheet.Cells[2, 4] = "错误信息";
sheet.Cells[2, 5] = "题名";
sheet.Cells[2, 6] = "条数";
sheet.Columns[2].ColumnWidth = 45;
sheet.Columns[3].ColumnWidth = 20;
sheet.Columns[4].ColumnWidth = 45;
sheet.Columns[5].ColumnWidth = 45;
sheet.Cells[2, 1].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;//水平居中
sheet.Cells[2, 2].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;//水平居中
sheet.Cells[2, 3].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;//水平居中
sheet.Cells[2, 4].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;//水平居中
sheet.Cells[2, 5].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;//水平居中
sheet.Cells[2, 6].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;//水平居中
//保存excel文件
book.SaveCopyAs(rootpath);
//关闭文件
book.Close(false, missing, missing);
//退出excel
app.Quit();
C#winfrom导出报表
最新推荐文章于 2023-02-17 13:26:14 发布