.Net DataTable导出到Excel
作者:互联网
/// <summary>
/// 打印方法
/// </summary>
/// <param name="dt">需要打印的数据表</param>
/// <param name="message">返回的具体文字信息</param>
/// <returns>返回打印是否成功</returns>
public static bool Print(DataTable dt, out string ls_msg)
{
bool bl_result = true;
if (dt == null)
{
bl_result = false;
ls_msg = "DataTable为null!";
return bl_result;
}
string saveFileName = "";
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "xls";
saveDialog.Filter = "Excel文件|*.xls|*.xlsx|*.*";
DialogResult dr = saveDialog.ShowDialog();
saveFileName = saveDialog.FileName;
//被点了取消
if (saveDialog.ShowDialog() != DialogResult.OK || saveFileName.IndexOf(":") < 0)
{
bl_result = false;
ls_msg = "";
return bl_result;
}
if (System.IO.File.Exists(saveFileName))//检察文件是否已存在,存在就把之前的删掉
{
try
{
File.Delete(saveFileName);
}
catch (Exception ex)
{
bl_result = false;
ls_msg = ex.Message;
return bl_result;
}
}
// 创建Excel应用程序对象
Microsoft.Office.Interop.Excel.Application excApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workBook = excApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet workSeet = workBook.Worksheets[1]; //取得sheet1
Microsoft.Office.Interop.Excel.Range range = null;
int tableCount = dt.Rows.Count;
int rowRead = 0;
float percent = 0;
for (int i = 0; i < dt.Columns.Count; i++)
{
workSeet.Cells[1, i + 1] = dt.Columns[i].ColumnName;
//设置标题的样式
range = (Microsoft.Office.Interop.Excel.Range)workSeet.Cells[1, i + 1];
range.Font.Bold = true; //粗体
range.Font.Size = 12;//字体大小
range.Font.Name = "宋体";
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); //背景色
range.EntireColumn.AutoFit(); //自动设置列宽
range.EntireRow.AutoFit(); //自动设置行高
}
for (int r = 0; r < dt.Rows.Count; r++)
{
for (int c = 0; c < dt.Columns.Count; c++)
{
//写入内容
workSeet.Cells[r + 2, c + 1] = "'" + dt.Rows[r][c].ToString();
//设置样式
range = (Microsoft.Office.Interop.Excel.Range)workSeet.Cells[r + 2, c + 1];
range.Font.Size = 10; //字体大小
range.Font.Name = "宋体";
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)) / tableCount;
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(saveFileName);
}
catch { }
workBook.Close();
if (excApp != null)
{
excApp.Workbooks.Close();
excApp.Quit();
int generation = System.GC.GetGeneration(excApp);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excApp);
excApp = 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
ls_msg = "成功";
return true;
}
WXRlalala
发布了7 篇原创文章 · 获赞 0 · 访问量 51
私信
关注
标签:Office,Interop,Excel,Microsoft,range,dt,Net,DataTable 来源: https://blog.csdn.net/qq_38596402/article/details/104516443