从数据集中获取信息并放入工作表中?
我遇到了以下一些问题:
我给我的方法一个数据集,它应该将信息放入 Excel 文件中,这是我想要的格式的模板。我创建的 Excel 文件有一个标题、一些过滤器和类似的东西,我设置了在标题之后填充文件的方法,等等,但问题是,当我这样做时,我丢失了我在模板。我正在使用此类http://www.codeproject.com/KB/office/biffcsharp。 .aspx .我不确定,可能该类的实现格式非常简单,或者它覆盖了我拥有的所有信息。
我的方法看起来像这样,使用上面链接上的类:
public void PopularSheet()
{
string filename = "C:\\test" + System.Web.HttpContext.Current.Session["SYSTEMCLIENTID"].ToString()+ System.Web.HttpContext.Current.Session["SYSTEMUSERTYPEID"].ToString()+ System.Web.HttpContext.Current.Session["CLIENTID"].ToString()+".xls";
File.Copy("C:\\test.xls", filename);
FileStream stream = new FileStream(filename, FileMode.OpenOrCreate);
ExcelWriter writer = new ExcelWriter(stream);
DataSet ds = GetDataSet();
writer.BeginWrite();
int jValue = ds.Tables[0].Columns.Count;
int iValue = ds.Tables[0].Rows.Count;
// Passa os dados do dataset para a planilha
for (int i = 0; i < iValue; i++)
{
// Lê todas as colunas da linha i
for (int j = 0; j < jValue; j++)
{
writer.WriteCell(i+2, j, ds.Tables[0].Rows[i][j].ToString());
}
}
writer.EndWrite();
stream.Close();
}
我还尝试使用 Excel 库,http://www.carlosag.net/tools/excelxmlwriter/,但我认为为了加载文件(以便我可以插入我需要的信息进去)我需要加载一个xml文件,这是不可能的!
我使用的另一个库在保存时出现了问题,我能够编辑工作表,然后当我保存并打开通过代码生成的 Excel 文件时,它会显示为空。
我不能使用任何强制我安装 Excel 的东西,这就是我尝试这些替代方案的原因。对于我能做什么有什么建议吗?
我需要做的:
- 加载现有的Excel文件作为“模板”
- 将数据集放入文件中
- 使用我用模板格式抛出的信息保存文件
I am having some issues with the following:
I give my method a dataset and it is supposed to throw the information into an excel file, which is a template of the formatting I desire. The excel file i created has a header, some filters and things of the sort, and I set my method to populate the file AFTER the header, etc but the problem is, when I do that, I lose all the formatting i had on the template. I am using this class http://www.codeproject.com/KB/office/biffcsharp.aspx . Im not sure, it may be that the format for the implementation of the class is a real simple one or that it overwrites all the information I had.
my method looks like this, using the class on the link above :
public void PopularSheet()
{
string filename = "C:\\test" + System.Web.HttpContext.Current.Session["SYSTEMCLIENTID"].ToString()+ System.Web.HttpContext.Current.Session["SYSTEMUSERTYPEID"].ToString()+ System.Web.HttpContext.Current.Session["CLIENTID"].ToString()+".xls";
File.Copy("C:\\test.xls", filename);
FileStream stream = new FileStream(filename, FileMode.OpenOrCreate);
ExcelWriter writer = new ExcelWriter(stream);
DataSet ds = GetDataSet();
writer.BeginWrite();
int jValue = ds.Tables[0].Columns.Count;
int iValue = ds.Tables[0].Rows.Count;
// Passa os dados do dataset para a planilha
for (int i = 0; i < iValue; i++)
{
// Lê todas as colunas da linha i
for (int j = 0; j < jValue; j++)
{
writer.WriteCell(i+2, j, ds.Tables[0].Rows[i][j].ToString());
}
}
writer.EndWrite();
stream.Close();
}
I also tried using an excel library,http://www.carlosag.net/tools/excelxmlwriter/,but i think in order to LOAD a file (so that i can insert the information i need into it) i need to load a xml file, which is impossible!
Another library I used presented a problem when saving, I was able to edit the worksheet and then when i saved and opened the excel file that was generated thru the code, it would come out empty .
I cannot use anything that will force me to install excel, which is why i am trying these alternatives. Are there any suggestions to what I could do?
What i need to do :
- Load an existing excel file as "template"
- Throw a dataset into the file
- Save the file with the information that i threw with the template format
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个名为 ClosedXML 的库,它对于创建 openxml 文件很有用。
There is a library called ClosedXML which is useful for creating openxml file.
快速思考:您可以使用 .csv 代替标准 .xls 文件吗?
如果是这样,您可以轻松填满您的桌子。
Quick thought: can you use a .csv instead of a standard .xls file?
If so, you can easily fill your table.