/// <summary>
/// 把DataTable导出到EXCEL
/// </summary>
/// <param name="reportName">报表名称</param>
/// <param name="dataTable">数据源表</param>
/// <param name="saveFileName">Excel全路径文件名</param>
/// <returns>导出是否成功</returns>
public bool ExportExcel(string reportName, System.Data.DataTable dataTable, string saveFileName)
{
if (dataTable == null)
{
returnStatus = -1;
returnMessage = "数据集为空!";
return false;
}
bool fileSaved = false;
Excel.Application xlApp = new Excel.Application();
if (xlApp == null)
{
returnStatus = -1;
returnMessage = "无法创建Excel对象,可能您的计算机未安装Excel!";
return false;
}
Workbooks workbooks = xlApp.Workbooks;
Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Worksheet worksheet = (Worksheet)workbook.Worksheets[1];//取得sheet1
worksheet.Cells.Font.Size = 10;
Range range;
long totalCount = dataTable.Rows.Count;
long rowRead = 0;
float percent = 0;
worksheet.Cells[1, 1] = reportName;
((Range)worksheet.Cells[1, 1]).Font.Size = 12;
((Range)worksheet.Cells[1, 1]).Font.Bold = true;
for (int i = 0; i < dataTable.Columns.Count; i++)
{
worksheet.Cells[2, i + 1] = dataTable.Columns[i].ColumnName;
range = (Range)worksheet.Cells[2, i + 1];
range.Interior.ColorIndex = 15;
range.Font.Bold = true;
}
for (int r = 0; r < dataTable.Rows.Count; r++)
{
for (int i = 0; i < dataTable.Columns.Count; i++)
{
worksheet.Cells[r + 3, i + 1] = dataTable.Rows[r][i].ToString();
}
rowRead++;
percent = ((float)(100 * rowRead)) / totalCount;
}
range = worksheet.get_Range(worksheet.Cells[2, 1], worksheet.Cells[dataTable.Rows.Count + 2, dataTable.Columns.Count]);
range.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic, null);
if (dataTable.Rows.Count > 0)
{
range.Borders[XlBordersIndex.xlInsideHorizontal].ColorIndex = XlColorIndex.xlColorIndexAutomatic;
range.Borders[XlBordersIndex.xlInsideHorizontal].LineStyle = XlLineStyle.xlContinuous;
range.Borders[XlBordersIndex.xlInsideHorizontal].Weight = XlBorderWeight.xlThin;
}
if (dataTable.Columns.Count > 1)
{
range.Borders[XlBordersIndex.xlInsideVertical].ColorIndex = XlColorIndex.xlColorIndexAutomatic;
range.Borders[XlBordersIndex.xlInsideVertical].LineStyle = XlLineStyle.xlContinuous;
range.Borders[XlBordersIndex.xlInsideVertical].Weight = XlBorderWeight.xlThin;
}
if (string.IsNullOrEmpty(saveFileName))
{
try
{
workbook.Saved = true;
workbook.SaveCopyAs(saveFileName);
fileSaved = true;
}
catch (Exception ex)
{
fileSaved = false;
returnStatus = -1;
returnMessage = "导出文件时出错,文件可能正被打开!\n" + ex.Message;
}
}
else
{
fileSaved = false;
}
if (range != null)
{
Marshal.ReleaseComObject(range);
range = null;
}
if (worksheet != null)
{
Marshal.ReleaseComObject(worksheet);
worksheet = null;
}
if (workbook != null)
{
Marshal.ReleaseComObject(workbook);
workbook = null;
}
if (workbooks != null)
{
Marshal.ReleaseComObject(workbooks);
workbooks = null;
}
xlApp.Application.Workbooks.Close();
xlApp.Quit();
if (xlApp != null)
{
Marshal.ReleaseComObject(xlApp);
xlApp = null;
}
GC.Collect();
return fileSaved;
}
把DataTable导出到EXCEL
于 2023-07-18 16:37:49 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)