C#代码动态创建多个工作表 [英] C# code to create multiple sheets dynamically

查看:133
本文介绍了C#代码动态创建多个工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我用一张纸创建了一个excel文件并将数据写入其中。



现在我的要求发生了变化,我必须创建一个excel文件并且

在运行时创建多个工作表并将数据写入不同的工作表。



for ex->



有时我必须创建20张excel表并将数据写入其中,有时我可能需要创建45表格并将数据写入其中。



我必须在运行时做这一切。

Hi All,

I have created an excel file with one sheet and wrote the data in it.

Now My requirement got changed and I have to create one excel file and
create multiple sheets at Runtime and write the data into different sheets.

for ex->

Sometimes I have to create 20 excel sheets and write the data into it and may be some times I have to create 45 sheets and write the data in to it.

All this I have to do at Runtime.

推荐答案

首先要定义所有变量来编写xlfile。你创建的编号不是固定的,所以将该值保存在一个变量中(例如.int nosheets = 45)。

现在部分是循环该变量,并在循环内保持代码添加那些没有像。

for(i = 0; i< nosheets; i ++)



ExcelWork

Sheet = ExcelWorkBoo k.Worksh

eets [i + 1];

//这里写下进一步的编码

Hi,first of all define all variables to write xlfile.The no.of sheet you creating is not fixed so keep that value in one variable(eg.int nosheets=45).
Now the part is to loop that variable and within loop keep code to add those no.of shets like.
                   for(i=0;i<nosheets;i++)
(
ExcelWork
Sheet = ExcelWorkBook.Worksh
eets[i + 1];
//here do write further coding
)


我建​​议使用Open XML SDK(而不是Office interop)。您可以按照这个有用的CodeProject文章来解释创建Excel工作簿的基本知识:

使用Open XML创建基本的Excel工作簿 [ ^ ]。



只有你可以创建几张纸,而不只是一张。 :-)



另请参阅这些Microsoft文章解释为什么使用Open XML SDK更可取:

http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2 [ ^ ],

http://support.microsoft.com/kb/257757/ en-us [ ^ ]。



参见:

http:/ /en.wikipedia.org/wiki/Office_Open_XML [ ^ ],

http://www.ecma-international.org /出版物/标准s / Ecma-376.htm [ ^ ]。



-SA
I would advise to use Open XML SDK (and not Office interop). You can follow this useful CodeProject article which explains the basic of creation of an Excel workbook:
Creating basic Excel workbook with Open XML[^].

Only you can create several sheets, not just one. :-)

See also these Microsoft article explaining why using Open XML SDK can be preferable:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2[^],
http://support.microsoft.com/kb/257757/en-us[^].

See also:
http://en.wikipedia.org/wiki/Office_Open_XML[^],
http://www.ecma-international.org/publications/standards/Ecma-376.htm[^].

—SA


这篇关于C#代码动态创建多个工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆