/// 导出数据到Excel中
/// </summary>
/// <param name="dt">DataTable数据源</param>
/// <returns></returns>
public
void
ExportToExcel(System.Data.DataTable dt)
{
if
(dt ==
null
)
return
;
Microsoft.Office.Interop.Excel.Application xlApp =
new
Microsoft.Office.Interop.Excel.Application();
if
(xlApp ==
null
)
{
XtraMessageBox.Show(
"无法创建Excel对象,可能您的电脑未安装Excel"
,
"系统提示"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
return
;
}
System.Windows.Forms.SaveFileDialog saveDia =
new
SaveFileDialog();
saveDia.Filter =
"Excel|*.xls"
;
saveDia.Title =
"导出为Excel文件"
;
if
(saveDia.ShowDialog() == System.Windows.Forms.DialogResult.OK && !
string
.Empty.Equals(saveDia.FileName))
{
Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
Microsoft.Office.Interop.Excel.Range range =
null
;
long
totalCount = dt.Rows.Count;
long
rowRead = 0;
float
percent = 0;
string
fileName = saveDia.FileName;
for
(
int
i = 0; i < dt.Columns.Count; i++)
{
worksheet.Cells[1, i + 1] = dt.Columns[i].ColumnName;
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, i + 1];
range.Font.Bold =
true
;
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic,
null
);
}
for
(
int
r = 0; r < dt.DefaultView.Count; r++)
{
for
(
int
i = 0; i < dt.Columns.Count; i++)
{
worksheet.Cells[r + 2, i + 1] = dt.DefaultView[r][i];
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[r + 2, i + 1];
range.Font.Size = 9;
range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic,
null
);
range.EntireColumn.AutoFit();
}
rowRead++;
percent = ((
float
)(100 * rowRead)) / totalCount;
System.Windows.Forms.Application.DoEvents();
}
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin;
if
(dt.Columns.Count > 1)
{
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin;
}
try
{
workbook.Saved =
true
;
workbook.SaveCopyAs(fileName);
}
catch
(Exception ex)
{
XtraMessageBox.Show(
"导出文件时出错,文件可能正被打开!\n"
+ ex.Message,
"系统提示"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
return
;
}
workbooks.Close();
if
(xlApp !=
null
)
{
xlApp.Workbooks.Close();
xlApp.Quit();
int
generation = System.GC.GetGeneration(xlApp);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp =
null
;
System.GC.Collect(generation);
}
GC.Collect();
#region 强行杀死最近打开的Excel进程
System.Diagnostics.Process[] excelProc = System.Diagnostics.Process.GetProcessesByName(
"EXCEL"
);
System.DateTime startTime =
new
DateTime();
int
m, killId = 0;
for
(m = 0; m < excelProc.Length; m++)
{
if
(startTime < excelProc[m].StartTime)
{
startTime = excelProc[m].StartTime;
killId = m;
}
}
if
(excelProc[killId].HasExited ==
false
)
{
excelProc[killId].Kill();
}
#endregion
XtraMessageBox.Show(
"导出成功"
,
"系统提示"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}