我有一个设计excel表我想从网格视图复制每一行到excel现有表。在网格视图中,我有相同的列,我的设计excel中有列。我不想导出到excel我想将我的数据复制到现有的excel表中,如果有方法请分享。
答案 0 :(得分:2)
您可以使用EasyXLS Excel库。以下代码从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);
如果您需要格式化数据,有关详情,请查看有关how to export GridView to Excel using EasyXLS。
的链接