【问题标题】:Excel Sheet copy from gridview in formated excel fileExcel 表格从 gridview 复制到格式化的 excel 文件中
【发布时间】:2015-05-05 03:04:02
【问题描述】:

我有一个设计 excel 表,我想将网格视图中的每一行复制到 excel 现有表中。在网格视图中,我的列与我的设计 excel 中的列相同。我不想导出到 excel 我想将我的数据复制到现有的 excel 表中,如果有办法,请分享。

【问题讨论】:

  • 到目前为止你搜索了什么?

标签: c# excel


【解决方案1】:

您可以使用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 的链接。

【讨论】:

  • 如何将特定的网格视图列导出到格式化的excel?例如,如果网格视图包含 6 列,但在我的格式化 excel 中我只有 3 列?
  • 如果您不想更改gridview源,最简单的解决方案是创建DataTable的副本(DataTable.Copy方法)并删除多余的列(DataTable.Columns.Remove方法) .或者,您可以使用 GridView 中的数据逐个单元格地填充 Excel 文件:easyxls.com/manual/basics/import-export-excel-data.html
  • 谢谢朋友,有什么问题我会检查并回复你
猜你喜欢
  • 1970-01-01
  • 2014-10-01
  • 1970-01-01
  • 2023-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多