Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将数据写入多个excel工作表_C#_Excel_Datatable_Asp.net 3.5 - Fatal编程技术网

C# 如何将数据写入多个excel工作表

C# 如何将数据写入多个excel工作表,c#,excel,datatable,asp.net-3.5,C#,Excel,Datatable,Asp.net 3.5,我有三个数据表。我需要把这三个数据表数据写在excel表格中。 正如我们所知,在一张excel表格下,我可以拥有(多于一张) 以同样的方式,我需要将我的3个数据表数据写入一个excel(其中,表1将包含在dattable1中,表2将包含在dattable2中,表3将包含在dattable3中) 这是我用来将数据表数据写入网格然后写入excel的代码 private void DataTableToExcel(DataTable dtResult) { tr

我有三个数据表。我需要把这三个数据表数据写在excel表格中。 正如我们所知,在一张excel表格下,我可以拥有(多于一张)

以同样的方式,我需要将我的3个数据表数据写入一个excel(其中,表1将包含在dattable1中,表2将包含在dattable2中,表3将包含在dattable3中)

这是我用来将数据表数据写入网格然后写入excel的代码

private void DataTableToExcel(DataTable dtResult)
        {
            try
            {
                DataGrid grid = new DataGrid();
                grid.HeaderStyle.Font.Bold = true;
                grid.DataSource = dtResult;
                grid.DataBind();
                // render the DataGrid control to a file
                using (StreamWriter sw = new StreamWriter(Server.MapPath("Report/Report.xls")))
                {
                    using (HtmlTextWriter hw = new HtmlTextWriter(sw))
                    {
                        grid.RenderControl(hw);
                    }
                }
                string filePath = Server.MapPath("~/" + "Report" + "/" + "Report.xls");
                System.IO.FileInfo targetFile = new System.IO.FileInfo(filePath);
                if (targetFile.Exists)
                {
                    Response.Clear();
                    string shortDate = DateTime.Now.ToShortDateString();
                    // Response.AddHeader("Content-Disposition", "attachment; filename=" + "Test");
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + "Emp" + shortDate + ".xls");
                    Response.AddHeader("Content-Length", targetFile.Length.ToString());
                    Response.ContentType = "application/vnd.ms-excel";
                    Response.WriteFile(targetFile.FullName);
                }
            }
            catch (Exception ex)
            {
                WriteLogError(ex.Message);
            }
        }
在此问题上的任何帮助都将不胜感激。
谢谢。

您可以使用C#的NPOI来实现这一点。非常容易使用

请看以下链接:

您可以使用以下内容引用工作簿中的每张工作表:

 // Getting the worksheet by its name...
 HSSFSheet sheet = templateWorkbook.GetSheet("Sheet1");