这是我使用COM interop生成Excel的代码。如何使用COM introp将丰富多彩的表格添加到Excel中?
public static void ExportDataTableToExcel(DataTable dt, string filepath)
{
object missing = Type.Missing;
object misValue = System.Reflection.Missing.Value;
//create excel
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
//add excel workbook
Microsoft.Office.Interop.Excel.Workbook wb = excel.Workbooks.Add();
//add worksheet to worlbook
Microsoft.Office.Interop.Excel.Worksheet ws = wb.Sheets[1] as Microsoft.Office.Interop.Excel.Worksheet;
//Set the header-row bold
ws.Range["A1", "A1"].EntireRow.Font.Bold = true;
//Adjust all columns
ws.Columns.AutoFit();
//spit top row
ws.Application.ActiveWindow.SplitRow = 1;
//freeze top row
ws.Application.ActiveWindow.FreezePanes = true;
int rowCount = 1;
foreach (DataRow dr in dt.Rows)
{
rowCount += 1;
for (int i = 1; i < dt.Columns.Count + 1; i++)
{
// Add the header the first time through
if (rowCount == 2)
{
ws.Cells[1, i] = dt.Columns[i - 1].ColumnName;
}
ws.Cells[rowCount, i] = dr[i - 1].ToString();
}
}
wb.SaveAs(@"C:\ErangaExcelTest.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue,
misValue, misValue, misValue,
Excel.XlSaveAsAccessMode.xlExclusive, misValue,
misValue, misValue, misValue, misValue);
wb.Close(missing, missing, missing);
excel.Quit();
}
这是我从这种方法得到的Excel。
我需要一个丰富多彩的表像这样
什么样的修改,做我需要做的有这种丰富多彩的Excel的?
是否可以加粗表格边框? –
更新了有关如何设置边框的信息。 –