【问题标题】:ASP.NET - Ability to Export CSV/excel to Multiple sheets?ASP.NET - 能够将 CSV/excel 导出到多个工作表?
【发布时间】:2011-11-30 05:05:25
【问题描述】:

我想知道是否有人能够将数据导出到多张工作表中的 excel 中......到目前为止,我只知道如何将多个对象导出到一张工作表中!

我正在使用 Devexpress 导出器?似乎他们不支持这一点,我也可以使用通用解决方案吗?

任何指导将不胜感激!

【问题讨论】:

    标签: c# asp.net excel export devexpress


    【解决方案1】:

    Epplus ?

    http://epplus.codeplex.com/releases/view/42439

    我在 winforms 应用程序中使用了它,它很棒。它只处理 xlsx 文件 - 即 Excel 2007 及更高版本。

    安德鲁

    【讨论】:

      【解决方案2】:

      文件路径应该是 server.mappath() 因为生成的 excel 应该首先保存在服务器路径中,并且可以允许下载。
      添加引用 Excel 并添加命名空间“使用 Excel”。

         public void Export(DataSet ds, string filePath)
          {
              string data = null;
              string columnName = null;
              int i = 0;
              int j = 0;
              Excel.Application xlApp;
              Excel.Workbook xlWorkBook;
              //Excel.Worksheet xlWorkSheet;
              Excel.Worksheet xlWorkSheet = null;
              object misValue = System.Reflection.Missing.Value;
              Excel.Range range;
      
              xlApp = new Excel.ApplicationClass();
              xlWorkBook = xlApp.Workbooks.Add(misValue);
              //xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
      
      
              for (int l = 0; l < ds.Tables.Count; l++)
              {
                  xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(l + 1);                
                  xlWorkSheet.get_Range("A1:D1", Type.Missing).Merge(Type.Missing);
                  xlWorkSheet.get_Range("A1", "D1").Font.Bold = true;
                  xlWorkSheet.Cells.Font.Name = "Courier New";
      
                  for (i = 0; i <= ds.Tables[l].Rows.Count - 1; i++)
                  {
      
      
                      for (j = 0; j <= ds.Tables[l].Columns.Count - 1; j++)
                      {
                          columnName = ds.Tables[l].Columns[j].ColumnName.ToString();
                          xlWorkSheet.Cells[3, j + 1] = columnName;
                          data = ds.Tables[l].Rows[i].ItemArray[j].ToString();
                          xlWorkSheet.Cells[i + 5, j + 1] = data;
                      }
                  }
              }
      
              xlWorkBook.SaveAs(filePath, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
              xlWorkBook.Close(true, misValue, misValue);
              xlApp.Quit();
      
              // kill all excel processes
              Process[] pros = Process.GetProcesses();
              for (int p = 0; p < pros.Length; p++)
              {
                  if (pros[p].ProcessName.ToLower().Contains("excel"))
                  {
                      pros[p].Kill();
                      break;
                  }
              }
      
              releaseObject(xlWorkSheet);
              releaseObject(xlWorkBook);
              releaseObject(xlApp);
          }
      
          private void releaseObject(object obj)
          {
              try
              {
                  System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                  obj = null;
              }
              catch (Exception ex)
              {
                  obj = null;
              }
              finally
              {
                  GC.Collect();
              }
          }
      

      【讨论】:

        【解决方案3】:

        去年初我也遇到了同样的问题。最终,我破解了自己的方法,自动生成了一个 .xlt Excel 文件,其中包含 Excel 可以接受的多个电子表格(实际上是网页)。

        我知道它不漂亮,但它对我有用。如果您决定尝试一下,请告诉我它是否也适合您。

        输出以下内容(您可以调整为任意数量的工作表):

        MIME-Version: 1.0
        X-Document-Type: Workbook
        Content-Type: multipart/related; boundary="----=_NextPart_01CB53EC.0FFDF540"
        
        ------=_NextPart_01CB53EC.0FFDF540
        Content-Location: file:///C:/CE594991/Book1.htm
        Content-Transfer-Encoding: quoted-printable
        Content-Type: text/html; charset=utf-8"
        
        <html xmlns:v=3D"urn:schemas-microsoft-com:vml"
        xmlns:o=3D"urn:schemas-microsoft-com:office:office"
        xmlns:x=3D"urn:schemas-microsoft-com:office:excel"
        xmlns=3D"http://www.w3.org/TR/REC-html40">
        
        <head>
        
        <link id=3D"shLink" href=3D"Book1_files/Sheet1.htm">
        <link id=3D"shLink" href=3D"Book1_files/Sheet2.htm">
        <link id=3D"shLink" href=3D"Book1_files/Sheet3.htm">
        
        <xml>
         <x:ExcelWorkbook>
          <x:ExcelWorksheets>
           <x:ExcelWorksheet>
            <x:Name>Sheet1</x:Name>
            <x:WorksheetSource HRef=3D"Book1_files/Sheet1.htm"/>
           </x:ExcelWorksheet>
           <x:ExcelWorksheet>
            <x:Name>Sheet2</x:Name>
            <x:WorksheetSource HRef=3D"Book1_files/Sheet2.htm"/>
           </x:ExcelWorksheet>
           <x:ExcelWorksheet>
            <x:Name>Sheet3</x:Name>
            <x:WorksheetSource HRef=3D"Book1_files/Sheet3.htm"/>
           </x:ExcelWorksheet>
          </x:ExcelWorksheets>
         </x:ExcelWorkbook>
        </xml>
        </head>
        
        </html>
        

        对于每一页,输出:

        ------=_NextPart_01CB53EC.0FFDF540
        Content-Location: file:///C:/CE594991/Book1_files/{ your sheet name }".htm
        Content-Type: text/html; charset=utf-8
        
        <html>
        { your stuffs }
        </html>
        

        循环结束

        ------=_NextPart_01CB53EC.0FFDF540
        Content-Location: file:///C:/CE594991/Book1_files/filelist.xml
        Content-Transfer-Encoding: quoted-printable
        Content-Type: text/xml; charset="utf-8"
        
        <xml xmlns:o=3D"urn:schemas-microsoft-com:office:office">
         <o:MainFile HRef=3D"../Book1.htm"/>
         <o:File HRef=3D"Sheet1.htm"/>
         <o:File HRef=3D"Sheet2.htm"/>
         <o:File HRef=3D"Sheet3.htm"/>
        </xml>
        ------=_NextPart_01CB53EC.0FFDF540--
        

        在您的代码中,将响应缓冲区设置为 false,将内容类型设置为“application/vnd.ms-excel”并添加标题“Content-Disposition”、“inline; filename={ your file name }”。请注意,输出文件的第一行之前不能有换行符。

        【讨论】:

          【解决方案4】:

          我正在使用 Devexpress 导出器?好像他们不支持这个,我 也可以使用通用解决方案吗?

          您在尝试XlsxExportOptions.ExportMode 属性吗? SingleFilePageByPage 导出模式允许您将文档逐页导出到同一 XLSX 文件中的多个工作表。另请看以下文章:How to export a report to the different sheets in the XLS file

          【讨论】:

            【解决方案5】:

            查看http://www.devexpress.com/example=E2440,了解您的请求。

            看来正是你要找的。​​p>

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2011-11-29
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2010-11-07
              • 2017-11-30
              • 2014-03-19
              • 1970-01-01
              相关资源
              最近更新 更多