从数据集中获取信息并放入工作表中?

发布于 2024-12-20 07:28:45 字数 1644 浏览 1 评论 0原文

我遇到了以下一些问题:

我给我的方法一个数据集,它应该将信息放入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌上芳菲 2024-12-27 07:28:45

有一个名为 ClosedXML 的库,它对于创建 openxml 文件很有用。

There is a library called ClosedXML which is useful for creating openxml file.

疧_╮線 2024-12-27 07:28:45

快速思考:您可以使用 .csv 代替标准 .xls 文件吗?
如果是这样,您可以轻松填满您的桌子。

Quick thought: can you use a .csv instead of a standard .xls file?
If so, you can easily fill your table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文