C# 根据Excel模版导出文件

string fileName = Path.Combine(Application.StartupPath, "kfwh_yhxf.xls");

Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application(); 
if (app == null)
{
    MessageBox.Show("对不起,您的电脑没有安装Excel,下发失败!");
    return;
}
app.Visible = false;
app.UserControl = true;
app.DisplayAlerts = false;
app.AlertBeforeOverwriting = false;            
Microsoft.Office.Interop.Excel.Workbooks workbooks = app.Workbooks;

Microsoft.Office.Interop.Excel._Workbook workbook = workbooks.Add(fileName); //加载模板
Microsoft.Office.Interop.Excel.Sheets sheets = workbook.Sheets;
Microsoft.Office.Interop.Excel._Worksheet worksheet = (Microsoft.Office.Interop.Excel._Worksheet)sheets.get_Item(1); //第一个工作薄。

if (worksheet == null)
{
    return;  //工作薄中没有工作表.
}
try
{
    worksheet.Cells[11, 1] = "1";           
    worksheet.Cells[11, 2] = "123";         
    worksheet.Cells[11, 3] = "123";         
    worksheet.Cells[11, 4] = "123";         
    worksheet.Cells[11, 5] = "备注";        
    worksheet.Cells[11, 6] = "";            
    app.SaveWorkspace(Type.Missing);
}
catch (Exception exp)
{
    // MessageBox.Show(exp.Message);
}
finally
{
    app.Quit();
    app = null;
}

 

posted @ 2016-08-24 17:07  Ran0  阅读(1783)  评论(0编辑  收藏  举报