Excel:写入Excel-单纯写入

//创建一个Excel.Application的新进程
Microsoft.Office.Interop.Excel.Application app = new Application();
if (app == null) return;
app.Visible = false;
app.UserControl = true;
Workbooks workbooks = app.Workbooks;
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);//这里的Add方法里的参数就相当于继承了一个空模板(暂这样理解吧)
Sheets sheets = workbook.Worksheets;
_Worksheet worksheet = (_Worksheet)sheets.get_Item(1);
if (worksheet == null) return;
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, 3]).Merge(Type.Missing);//横向合并
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, 1]).Value2 = "导出EXCEL测试1";

worksheet.Cells[2, 1] = "序号";
worksheet.Cells[2, 2] = "公司";
worksheet.Cells[2, 3] = "部门";

worksheet.get_Range(worksheet.Cells[2, 1], worksheet.Cells[2, 3]).Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
int rowNum = 0;
for (int i = 0; i < 100; i++)
{
    rowNum = i + 1;
    worksheet.Cells[3 + i, 1] = rowNum;
    worksheet.Cells[3 + i, 2] = "公司" + (i + 1);
    worksheet.Cells[3 + i, 3] = "部门" + (i + 1);
    worksheet.get_Range(worksheet.Cells[3 + i, 1], worksheet.Cells[3 + i, 3]).Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);//设置边框颜色
}
worksheet.Name = "导出Excel测试1";
string tick = DateTime.Now.Ticks.ToString();
string save_path = "\\" + tick + ".xls";

workbook.SaveAs(save_path, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);       

posted @ 2012-02-22 14:38  pnljs  阅读(1754)  评论(0编辑  收藏  举报