2011-08-17 73 views
0

我有Excel(.xlsx)文件(模板),我需要從SQL服務器數據庫的Excel的某些部分填充數據。它是否可以兼容?如果它不是posibble實現它的最好方法是什麼?我也在考慮SQL Server Reporting Services。請告訴我。Excel報告與asp.net

感謝您的任何建議。

回答

1

有很多方法可以做到這一點,這真的取決於你更舒適的做以及如何將解決方案整合到最終解決方案中。

這裏有一些方法我用,給你什麼可以做的想法...

報告服務

的控制是很容易集成到現有的ASP.NET解決方案,安全性基於Windows身份驗證,因此安全性在SQL Server/Active Directory中進行管理。通常在我的小應用程序中,我使用模擬來禁用安全性並設置隱藏報告參數來控制報告。報告使用GUI在BIDS/VS中創建,並且非常像在Access中構建報告。該控件還支持大量不同格式的導出(PDF,XLS,DOC等)。

編輯XLS文件,而Excel中

我一直在使用NPOI在過去的幾年裏,它的偉大,很容易管理/處理像模板XLS文件。以下是一個template in NPOI的示例。

// Open Template 
FileStream fs = new FileStream(Server.MapPath(@"\template\Template_EventBudget.xls"), FileMode.Open, FileAccess.Read); 

// Load the template into a NPOI workbook 
HSSFWorkbook templateWorkbook = new HSSFWorkbook(fs, true); 

// Load the sheet you are going to use as a template into NPOI 
HSSFSheet sheet = templateWorkbook.GetSheet("Event Budget"); 

// Insert data into template 
sheet.GetRow(1).GetCell(1).SetCellValue(EventName.Value); // Inserting a string value into Excel 
sheet.GetRow(1).GetCell(5).SetCellValue(DateTime.Parse(EventDate.Value)); // Inserting a date value into Excel 

sheet.GetRow(5).GetCell(2).SetCellValue(Double.Parse(Roomandhallfees.Value)); 

直接XLS/XLSX與OLE

另一種解決方案,以直接與XLS和XLSX文件工作是使用其上安裝與2007 Office System Driver的OleDbConnection。

using (
      OleDbConnection con = 
       new OleDbConnection(
        @"Provider=Microsoft.ACE.OLEDB.12.0; Extended Properties=Excel 12.0;Data Source=" + SourceFile + 
        @";Extended Properties=Excel 12.0;")) 
     { 
      con.Open(); 
      string sql = String.Format("SELECT * FROM [{0}$]", sheetName); 
      OleDbDataAdapter da = new OleDbDataAdapter(sql, con); 

      da.Fill(dt); 
     } 

使用SSIS來填充Excel模板

這是所有SQL Server中完成的,輸出可以郵寄或文件夾中刪除。如果我需要使用特殊格式/公式/等等來自動發送每個定義的Excel文件,那麼我使用這種方法...這是使用SSIS包中的BIDS/VS進行設置的。您將創建一個帶有Excel Destination的數據流。這種方法有一些限制,請使用鏈接獲取詳細信息。

同樣,正確的解決方案將基於您的需求...同時考慮到維護,因爲報告的最終消費者似乎總是需要對報告進行更改/更新!

1

我使用了Spreadsheet齒輪。它是有償的,但是一個非常好的工具。