vs2010 C#如何将内容写入EXCEL不同的SHEET表里?

hlzyly hlzyly 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
各位大哥:
我现使用VS2010 旗舰版,在编写一段代码实现将数组的内容写入一个EXCEL文件中的不同Sheet表里时,总是失败,请大家能否给一段成功运行的代码呀?谢谢了。下面贴上我的代码段,也请大家帮忙看看为什么失败?
private void createxcel()
{

Excel.Application xlapp1 = default(Excel.Application);
//Excel对象
Excel.Workbook xlbook1 = default(Excel.Workbook);
//工作簿
Excel.Worksheet xlsheet1 = default(Excel.Worksheet);
//工作表


try
{
xlapp1 = new Excel.Application();
xlapp1.Visible = false;
xlapp1.DisplayAlerts = false;
xlapp1.ShowWindowsInTaskbar = false;

xlbook1 = xlapp1.Workbooks.Add() ;
//新建EXCEL工件簿文件
xlsheet1 = xlbook1.Worksheets[1];//这句代码总是出错,无法通过编译,该怎么写呢?

xlsheet1.Cells[1, 1] = "流水号";
xlsheet1.Cells[1, 1] = "流水号";
xlsheet1.Cells[1, 2] = "名称";
xlsheet1.Cells[1, 3] = "主题";
xlsheet1.Cells[1, 4] = "时间";
xlsheet1.Cells[1, 5] = "时限";
xlsheet1.Cells[1, 6] = "状态";
xlsheet1.Cells[1, 7] = "时间";
xlsheet1.Cells[1, 20] = "地址";
xlsheet1.Name = DateTime.Now.Year.ToString() + "年";
xlsheet1.Cells.Font.Size = 10;
xlsheet1.Cells.Font.Bold = false;

xlsheet1 = xlbook1.Worksheets[2];//和上面的sheet不同。

xlsheet1.Cells[1, 1] = "流水号";
xlsheet1.Cells[1, 2] = "主题";
xlsheet1.Cells[1, 3] = "时间";
xlsheet1.Cells[1, 4] = "时限";
xlsheet1.Cells[1, 5] = "状态";
xlsheet1.Cells[1, 6] = "时间";
xlsheet1.Cells[1, 20] = "地址";
xlsheet1.Name = DateTime.Now.Year.ToString() + "年";
xlsheet1.Cells.Font.Size = 10;
xlsheet1.Cells.Font.Bold = false;

xlbook1.SaveAs(datafile + ".xls");
//保存文件

}
catch (Exception ex)
{
log(ex.Message.ToString());
}
finally
{
xlapp1.Workbooks.Close();
xlapp1.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlapp1);
xlsheet1 = null;
xlbook1 = null;
xlapp1 = null;
System.GC.Collect(0);
}


}
...全文

相关推荐

评论 15
收藏 已收藏
分享
返回顶部
hlzyly
C# 111,130 成员 642,541 内容