//do the new excel document work using the background worker private void bg_DoWork(object sender, DoWorkEventArgs e) { DataTable dtExport = this.dataSource; if (TableToExport != null && isExportAllPage) { dtExport = TableToExport; } string outError = ""; AsposeExcelTools.DataTableToExcel2(dtExport, (String)e.Argument, out outError); }
/// <summary> /// 使用背景线程导出Excel文档 /// </summary> private void bg_DoWork(object sender, DoWorkEventArgs e) { DataTable table = new DataTable(); if (AllToExport != null && isExportAllPage) { if (AllToExport is DataView) { DataView dv = (DataView)AllToExport;//默认导出显示内容 table = dv.ToTable(); } else if (AllToExport is DataTable) { table = AllToExport as DataTable; } else { table = ReflectionUtil.CreateTable(AllToExport); } //解析标题 string originalName = string.Empty; foreach (DataColumn column in table.Columns) { originalName = column.Caption; if (columnNameAlias.ContainsKey(originalName.ToUpper())) { column.Caption = columnNameAlias[originalName.ToUpper()]; column.ColumnName = columnNameAlias[originalName.ToUpper()]; } } //for (int i = 0; i < this.gridView1.Columns.Count; i++) //{ // if (!this.gridView1.Columns[i].Visible) // { // table.Columns.Remove(this.gridView1.Columns[i].FieldName); // } //} } else { DataColumn column; DataRow row; for (int i = 0; i < this.gridView1.Columns.Count; i++) { if (this.gridView1.Columns[i].Visible) { column = new DataColumn(this.gridView1.Columns[i].FieldName, typeof(string)); column.Caption = this.gridView1.Columns[i].Caption; table.Columns.Add(column); } } object cellValue = ""; string fieldName = ""; for (int i = 0; i < gridView1.RowCount; i++) { row = table.NewRow(); for (int j = 0; j < gridView1.Columns.Count; j++) { if (this.gridView1.Columns[j].Visible) { fieldName = gridView1.Columns[j].FieldName; cellValue = gridView1.GetRowCellValue(i, fieldName); row[fieldName] = cellValue ?? ""; } } table.Rows.Add(row); } } string outError = ""; AsposeExcelTools.DataTableToExcel2(table, (String)e.Argument, out outError); }