【问题标题】:How to export to excel in windows app C#?如何在 Windows 应用程序 C# 中导出到 excel?
【发布时间】:2023-03-17 13:55:01
【问题描述】:

我是 Windows 应用程序开发的新手,正在尝试编写将数据集导出到 excel 的代码。我已经在互联网上阅读了几个关于此的示例,但所有示例都将文件导出到某个位置。我想像在网络中一样导出文件(作为打开要求保存为文件)。
下面是我的 Web 应用代码,我们需要做哪些更改才能在窗口上运行它?

DataGridView grid_records = new DataGridView();
grid_records.DataSource = dset;
//grid_records.DataBind();

Response.Clear();

Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = "";
string FileName = "UIDAI" + DateTime.Now + ".xls";
StringWriter strwritter = new StringWriter();
HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
grid_records.GridLines = GridLines.Both;
grid_records.HeaderStyle.Font.Bold = true;
grid_records.RenderControl(htmltextwrtter);
Response.Write(strwritter.ToString());
//Response.End();

Response.Flush();
Response.SuppressContent = true;
HttpContext.Current.ApplicationInstance.CompleteRequest();

【问题讨论】:

  • .net Web 框架不同于桌面应用程序。您不需要使用Response 类,您只需要使用fileSaveDialog 将文件保存在所需的位置,并使用Process 类打开文件,如果系统中有fileReader,即PDF 阅读器、Excel 等即可。跨度>
  • @M Adeel Khalid,你能举个例子吗?

标签: c# winforms datagridview export-to-excel


【解决方案1】:

感谢这么多提示,下面是我转换为窗口应用程序后的最终代码
希望它会帮助别人

   private void btn_export_Click(object sender, EventArgs e)
    {
        try
        {
            SaveFileDialog savefile = new SaveFileDialog();
            savefile.FileName = "Response.xls";
            savefile.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";
            if (dset.Tables[0].Rows.Count > 0)
            {
                if (savefile.ShowDialog() == DialogResult.OK)
                {
                    //using (StreamWriter sw = new StreamWriter(savefile.FileName))
                    //    sw.WriteLine("Hello World!");
                    StreamWriter wr = new StreamWriter(savefile.FileName);
                    for (int i = 0; i < dset.Tables[0].Columns.Count; i++)
                    {
                        wr.Write(dset.Tables[0].Columns[i].ToString().ToUpper() + "\t");
                    }

                    wr.WriteLine();

                    //write rows to excel file
                    for (int i = 0; i < (dset.Tables[0].Rows.Count); i++)
                    {
                        for (int j = 0; j < dset.Tables[0].Columns.Count; j++)
                        {
                            if (dset.Tables[0].Rows[i][j] != null)
                            {
                                wr.Write(Convert.ToString(dset.Tables[0].Rows[i][j]) + "\t");
                            }
                            else
                            {
                                wr.Write("\t");
                            }
                        }
                        //go to next line
                        wr.WriteLine();
                    }
                    //close file
                    wr.Close();
                    MetroMessageBox.Show(this, "Data saved in Excel format at location "+ savefile.FileName , "Successfully Saved", MessageBoxButtons.OK, MessageBoxIcon.Question);
                }
            }
            else
            {
                MetroMessageBox.Show(this,"Zero record to export , perform a operation first","Can't export file",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }
        catch (Exception ex)
        {
            MetroMessageBox.Show(this, v1.PrintExceptionDetails(ex), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            el.LogError(ex);
        }
        finally
        {

        }
    }

【讨论】:

    【解决方案2】:

    在搜索与 Windows 桌面应用程序相关的答案时,请使用关键字“Winforms”、“C#”。

    您需要使用SaveFileDialog 类选择一个文件并使用FileStream 向其中写入内容。

    您可以找到详细的示例msdnherehere

    【讨论】:

      【解决方案3】:

      此示例将告诉您How to export Dataset to Excel,如果您在此link 上看到第二个答案,您将了解如何允许用户将文档保存在其 PC 的任何位置。你可以打开Excel文件by following this link

      在提出问题之前始终进行搜索,因为无论您提出什么问题,这里都可以找到所有内容。

      希望对你有帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-24
        • 2020-06-17
        • 1970-01-01
        • 1970-01-01
        • 2010-11-01
        • 2018-06-05
        • 2016-07-05
        • 2021-03-26
        相关资源
        最近更新 更多