internal Worksheet(Interop.Excel.Worksheet worksheet) { this.worksheet = worksheet; }
//导出Excel private void ExportExcel1() { // 定义要使用的Excel 组件接口 // 定义Application 对象,此对象表示整个Excel 程序 Interop.Excel.Application excelApp = null; Interop.Excel.Workbook workBook = null; Interop.Excel.Worksheet ws = null; Interop.Excel.Range r; int row = 1; int cell = 1; int i = 0; int Count = 0; try { //初始化 Application 对象 excelApp excelApp = new Interop.Excel.Application(); workBook = excelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet); ws = (Worksheet)workBook.Worksheets[1]; // 命名工作表的名称为 "Task Management" ws.Name = "Soukey导出数据"; // 遍历数据表中的所有列 for (i = 0; i < m_pData.Columns.Count; i++) { ws.Cells[row, cell] = m_pData.Columns[i].ColumnName; r = (Range)ws.Cells[row, cell]; ws.get_Range(r, r).HorizontalAlignment = Interop.Excel.XlVAlign.xlVAlignCenter; cell++; } Count = m_pData.Rows.Count; // 创建行,把数据视图记录输出到对应的Excel 单元格 for (i = 0; i < m_pData.Rows.Count; i++) { for (int j = 0; j < m_pData.Columns.Count; j++) { ws.Cells[i + 2, j + 1] = m_pData.Rows[i][j]; Range rg = (Range)ws.get_Range(ws.Cells[i + 2, j + 1], ws.Cells[i + 2, j + 1]); rg.EntireColumn.ColumnWidth = 20; rg.NumberFormatLocal = "@"; } //更新进度条信息 m_sender.BeginInvoke(m_senderDelegate, new object[] { Count, i, false }); } workBook.SaveCopyAs(m_FileName); workBook.Saved = true; } catch (System.Exception) { return; } finally { excelApp.UserControl = false; excelApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(ws); System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp); GC.Collect(); //更新进度条为完成,并传递参数表示导出任务完成 m_sender.BeginInvoke(m_senderDelegate, new object[] { Count, i, true }); } return; }