简体   繁体   English

从Excel表格复制Excel格式的Excel文件格式

[英]Excel Sheet copy from gridview in formated excel file

i have a design excel sheet i wants to copy each of rows from grid view to excel existing sheet. 我有一个设计Excel工作表,我想将网格视图中的每一行复制到Excel现有工作表中。 In grid view i have same columns that i have columns in my design excel. 在网格视图中,我的列与设计excel中的列相同。 I don't wants to exports into excel i wants to copy my data into existing excel sheet if there is a way so please share it. 我不想导出到excel,我想将数据复制到现有的excel表中(如果可以的话),请分享。

You can use EasyXLS Excel library. 您可以使用EasyXLS Excel库。 The below code get data from gridview and copy to an existing sheet: 下面的代码从gridview获取数据并将其复制到现有工作表中:

// Load the existing Excel file
ExcelDocument workbook = new ExcelDocument();
workbook.easy_LoadXLSFile(pathToExistingExcel);

// Get the first sheet
ExcelWorksheet sheet = (ExcelWorksheet)workbook.easy_getSheetAt(0);

// Create a dataset that keeps the gridview datatable
DataSet dataSet = new DataSet();
dataSet.Tables.Add((DataTable)gridView.DataSource);

// Insert gridview data into sheet
sheet.easy_insertDataSet(dataset, false);

// Choose a name for the xls file 
string fileName = "Excel.xls";
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
Response.ContentType = "application/vnd.ms-excel";

// Export new Excel file and prompt the "Open or Save Dialog Box"
workbook.easy_WriteXLSFile(Response.OutputStream);

If you have to format the data and for more details check this link about how to export GridView to Excel using EasyXLS . 如果您必须格式化数据并了解更多详细信息,请查看此链接,了解如何使用EasyXLS将GridView导出到Excel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM