这样我们就需要在对应的列表中写入循环就可以将数据一列列的写入到Excel表中了。
由于OleDb操作对Excel的控制性不高,所以要想把表格属性也控制好,这时候我们可以结合Excel组件来帮助我们完成,只要把处理数据这重要的部分交给OleDbl来操作即可
//设置表单样式
App = new Excel.Application(); //创建 Excel对象
object missing = Missing.Value; //获取缺少的object类型值
wb = App.Workbooks.Open(ExcelName, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
Excel.Worksheet ws1 = ((Excel.Worksheet)wb.Worksheets["刀具预估数量"]);//方法一:指定工作表名称读取
Excel.Worksheet ws2 = ((Excel.Worksheet)wb.Worksheets["Forecast"]);//方法一:指定工作表名称读取
ws1.get_Range("A1", "P1").Interior.ColorIndex = 14;//填充背景色
ws1.get_Range("A1", "P1").Borders.LineStyle = 1;//设置表格的线宽
ws2.get_Range("A1", "P1").Interior.ColorIndex = 14;//填充背景色
ws2.get_Range("A1", "P1").Borders.LineStyle = 1;//设置表格的线宽
ws1.get_Range("A1").ColumnWidth = 5; //固定宽度
ws1.get_Range("B1").ColumnWidth = 10;
ws1.get_Range("C1").ColumnWidth = 10;
ws1.get_Range("D1").ColumnWidth = 15;
ws2.get_Range("A1").ColumnWidth = 5; //固定宽度
ws2.get_Range("B1").ColumnWidth = 25;
ws2.get_Range("C1").ColumnWidth = 15;
wb.Save();
App.Visible = true;