2011-11-30 81 views
2

我想知道如果有人有一個解決方案,可以導出到多個工作表的Excel中的數據...截至目前,我只知道如何導出多個對象到一張單!ASP.NET - 能夠導出CSV/Excel到多個工作表?

我正在使用Devexpress出口商?似乎他們不支持這一點,我也可以使用一個通用的解決方案?

任何指導將不勝感激!

回答

0

文件路徑也許應該使用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(); 
     } 
    } 
0

去年年初我遇到了同樣的問題。最終,我能夠用自己的方式來自動生成具有多個可被Excel接受的電子表格(實際上是網頁)的.xlt 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> 

結束for循環

------=_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-Disp命題「,」內聯;文件名= {文件名}」。注意必須有輸出文件的第一行前不換行。

相關問題