文章分類/

Infragistics|Ultimate UI for ASP.NET|WebDataGrid:Excel 導入/導出(Excel、PDF、XPS)

尚無瀏覽量
2023-09-03 更新

infragistics log

我們收到許多有關 Excel 和用於通過 Infragistics 控件導入/導出數據的文檔 API 的問題。 因此,在這篇文章中,我想解釋一下最近發布的 WebDataGrid控件。目前,內置不支持將內容導出到 Excel、PDF 和 XPS,但這可能是將來支持的功能之一。

現在,讓我們看看如何使用 Excel API 的 load 方法從 Excel 檢索數據並在 WebDataGrid 中顯示該數據。 將數據連接到網格後,您可以使用各種功能,例如過濾、排序和編輯。如果用戶想要導出顯示的數據,可以使用相同的 API Engine 導出編輯後的數據並以 Excel、PDF 或 XPS 格式查看。

導入數據

首先,分配一個局部變量。

//Create the temporary table to store data DataTable myDataTable = new DataTable();    
DataColumn myDataColumn;   
DataRow myDataRow;    
  
//MIN/MAX Values used to frame the working size of the Excel data to be imported.    
int minCellRow = Int32.MaxValue;    
int maxCellRow = Int32.MinValue;    
int minCellColumn = Int32.MaxValue;    
int maxCellColumn = Int32.MinValue;  

接下來,使用包含 NorthWind 客戶數據的 Excel 工作表,調用 Excel API 的 Load 方法來加載數據並構建數據集。

Workbook internalWorkBook = Workbook.Load(Request.PhysicalApplicationPath + "Northwind.xls");  

Load 方法加載 Excel 內容並返回 Excel 工作簿對象。 返回工作簿後,您可以搜索包含數據的工作表並構造數據對象。將此數據對象綁定到 WebDataGrid。 此示例使用數據表。首先,您可以確定數據綁定和列結構,然後相應地構建數據對象的骨架。 您可以使用工作簿對象迭代行和列,並確定要加載到 WebDataGrid 中的最小/最大工作表單元格。

foreach (Infragistics.Excel.WorksheetRow row in internalWorkBook.Worksheets["Customers"].Rows)    
{   
    foreach (Infragistics.Excel.WorksheetCell cell in row.Cells)    
    {    
        if (cell.Value != null)   
        {    
           //Logic For Determining the Range of Rows/Columns in the Excel File.   
           minCellRow = Math.Min(minCellRow, cell.RowIndex);   
           maxCellRow = Math.Max(maxCellRow, cell.RowIndex);   
           minCellColumn = Math.Min(minCellColumn, cell.ColumnIndex);   
           maxCellColumn = Math.Max(maxCellColumn, cell.ColumnIndex);   
        }   
    }    
}  

一旦收集了工作表的結構信息,您就可以在數據庫中創建行時配置 WebDataGrid 的列。 您可以在此處編輯和刪除不想導入的列或您創建的 WebDataGrid 中不想設置其屬性的列。

for (int i = minCellColumn; i <= maxCellColumn; i++)    
{    
   //Get the column name    
   string columnName = internalWorkBook.Worksheets["Customers"].Rows[minCellRow].Cells[ i].Value.ToString();   
  
   //The export that was demonstrated earlier utilizes the first row   
   //for the column header. We can now use that to give column names.   
   myDataColumn = new DataColumn(columnName);   
  
   //Add the columns to the datatable.   
   myDataTable.Columns.Add(myDataColumn);   
  
   //Create WebDataGrid Columns and enable settings    
   BoundDataField bdf = new BoundDataField(true);    
   bdf.DataFieldName = columnName;    
   bdf.Key = columnName;    
   bdf.Header.Text = columnName;    
   bdf.Width = Unit.Pixel(100);    
   importGrid.Columns.Add(bdf); }  

最後將工作表中的數據推送到DataTable中

for (int rowIndex = minCellRow + 1; rowIndex <= maxCellRow; rowIndex++)    
{    
   //Create a new DataRow myDataRow = myDataTable.NewRow();    
   //Loop through the columns and associate the value to each cell    
   for (int columnIndex = minCellColumn; columnIndex <= maxCellColumn; columnIndex++)    
   {    
      myDataRow[columnIndex] = internalWorkBook.Worksheets["Customers"].Rows[rowIndex].Cells[columnIndex].Value;    
   }    
      
   //Add The Row to a DataTable    
   myDataTable.Rows.Add(myDataRow);    
}  

DataTable 和 WebDataGrid 列已準備就緒。接下來,綁定到 WebDataGrid。

//Set the primary key so that the WebDataGrid can perform Auto Crud    
myDataTable.PrimaryKey = new DataColumn[] { myDataTable.Columns["CustomerID"] };   
  
//ImportGrid below is the grid that we have on our page    
importGrid.DataSource = myDataTable; importGrid.DataBind();  

注意:該代碼假定 XLS 文件位於服務器上,因此可以將其輸入 Excel 引擎並導入到 WebDataGrid 中。如果要擴展行為,比如在客戶端導入本地Excel文件,則需要在客戶端添加一次將文件上傳到服務器的功能。上傳到服務器後,您可以使用導入方法提取數據。

導出數據

使用API​​導出不同格式的數據的概念是相同的。首先,為文檔創建一個列結構,迭代 WebDataGrid 列並導出它們。接下來,迭代 WebDataGrid 的行以創建要導出的數據行。導出為任何格式的代碼幾乎相同,因此我將在此處使用導出為 Excel 作為示例。

首先,創建要將數據導出到的工作簿和工作表對象。創建輔助變量以進一步完成任務。

//Create workbook and worksheet object for Excel Workbook    
theWorkbook = new Workbook();    
Worksheet theWorkSheet = theWorkbook.Worksheets.Add("WorkSheet1");    
int iRow = 1; int iCell = 1;  

下一步是迭代 WebDataGrid 列以在 Excel 工作表中創建一個列,並查找用於填充該列中的單元格的行。

//Iterate through the columns of the WebDataGrid and create   
 // columns within the worksheet that will be exported.    
foreach(GridField gridField in this.WebDataGrid1.Columns)    
{    
   iRow = 1;    
   theWorkSheet.Rows[iRow].Cells[iCell].Value = gridField.Header.Text;    
   theWorkSheet.Columns[iCell].Width = 5000;    
   iRow += 1;    
  
   //Now iterate through the grid rows to add rows to the worksheet    
   foreach(GridRecord gridRecord in this.WebDataGrid1.Rows)    
   {    
      theWorkSheet.Rows[iRow].Cells[iCell].Value = gridRecord.Items[iCell-1].Text;    
      iRow += 1;    
   }    
   iCell += 1;    
}  

附加的示例包括用於在導出時格式化單元格的附加代碼。 創建工作表並導出數據後,您可以將 Excel 工作表寫入流以在客戶端上查看。

//Create the Stream class    
System.IO.MemoryStream theStream = new System.IO.MemoryStream();    
  
//Write the in memory Workbook object to the Stream    
theWorkbook.Save(theStream);    
  
//Create a Byte Array to contain the stream and send the exported sheet to the client    
byte[] byteArr = (byte[])Array.CreateInstance(typeof(byte), theStream.Length);    
theStream.Position = 0;    
theStream.Read(byteArr, 0, (int)theStream.Length);    
theStream.Close();    
Response.Clear();    
Response.AddHeader("content-disposition", "attachment; filename=ExportedTo.xls");    
Response.BinaryWrite(byteArr);    
Response.End();  

完畢!

使用上面代碼示例中我們產品中包含的 API,您可以從 Excel 導入數據並將數據導出到 Excel、PDF 或 XPS。

快速跳轉目錄

✦ 群昱 AccessSoft 你的全面軟體解決方案 ✦

身為全球眾多知名軟體在台灣合作夥伴,歡迎諮詢你需要的軟體服務!

Picture of 軟體專家
軟體專家

群昱作為全球知名軟體推薦合作夥伴,致力於提供更多軟體解決方案給你!

更多軟體新知

立即詢價

請留下完整資訊,以便我們提供精確的服務內容給你。

詢價資訊