【转】C#导出EXCEL文档并杀掉进程最佳解决办法
转自:http://www.jiamaocode.com/Cts/1115.html
生面EXCEL的代码:
View Code
杀掉此进程的代码:
View Code
生面EXCEL的代码:
View Code
string[] headerName = new string[] { " ", "部门", "凭证类型", "财务月", "凭证号", "金额" };
Excel.Application xlApp = new Excel.ApplicationClass();
xlApp.DefaultFilePath = "";
xlApp.DisplayAlerts = true;
xlApp.SheetsInNewWorkbook = 1;
Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
Excel.Worksheet ws = (Excel.Worksheet)xlBook.Worksheets[1];
//ws.Name = strFileName;
try
{
int rowNum = FinishAccountDetailSummaryList.Count;
int columnNum = headerName.Length;
ws.get_Range(ws.Cells[1, 1], ws.Cells[rowNum + 1, columnNum]).Borders.LineStyle = Excel.XlLineStyle.xlContinuous;//边框样式
ws.get_Range(ws.Cells[1, 1], ws.Cells[1, columnNum]).Font.Bold = true;
ws.get_Range(ws.Cells[1, 1], ws.Cells[1, columnNum]).HorizontalAlignment = Excel.Constants.xlCenter;
ws.get_Range(ws.Cells[1, 1], ws.Cells[1, columnNum]).VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
ws.get_Range(ws.Cells[1, 1], ws.Cells[1, columnNum]).Interior.Color = 0xf8819a;//单元格背景颜色
object[,] objheader = new object[rowNum + 1, columnNum];
int j = 0;
foreach (string header in headerName)
{
objheader[0, j] = header;
j++;
}
char mycolumns = (char)(columnNum + 64);
Excel.Range r = ws.get_Range("A1", mycolumns.ToString() + "1");
//xlApp.ActiveCell.HorizontalAlignment = Excel.Constants.xlCenter;
//xlApp.ActiveCell.Font.Bold = true;
r.Value2 = objheader;
for (int i = 0; i < rowNum; i++)
{
objheader[0, 0] = "";
objheader[0, 1] = FinishAccountDetailSummaryList[i].BuMen;
objheader[0, 2] = FinishAccountDetailSummaryList[i].PingZhengLeiXing;
objheader[0, 3] = FinishAccountDetailSummaryList[i].PingZhengRiQi;
objheader[0, 4] = FinishAccountDetailSummaryList[i].PingZhengHao;
objheader[0, 5] = FinishAccountDetailSummaryList[i].JinEr;
r = ws.get_Range("A" + (i + 2), mycolumns.ToString() + (i + 2));
xlApp.ActiveCell.Borders.Weight = 1;
r.Value2 = objheader;
}
r.EntireColumn.AutoFit();
xlBook.SaveCopyAs(strFileName);
}
catch (Exception ex)
{
throw ex;
}
finally
{
killexcel(xlApp);//调用杀进程的方法,如下:
GC.Collect();
}
Excel.Application xlApp = new Excel.ApplicationClass();
xlApp.DefaultFilePath = "";
xlApp.DisplayAlerts = true;
xlApp.SheetsInNewWorkbook = 1;
Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
Excel.Worksheet ws = (Excel.Worksheet)xlBook.Worksheets[1];
//ws.Name = strFileName;
try
{
int rowNum = FinishAccountDetailSummaryList.Count;
int columnNum = headerName.Length;
ws.get_Range(ws.Cells[1, 1], ws.Cells[rowNum + 1, columnNum]).Borders.LineStyle = Excel.XlLineStyle.xlContinuous;//边框样式
ws.get_Range(ws.Cells[1, 1], ws.Cells[1, columnNum]).Font.Bold = true;
ws.get_Range(ws.Cells[1, 1], ws.Cells[1, columnNum]).HorizontalAlignment = Excel.Constants.xlCenter;
ws.get_Range(ws.Cells[1, 1], ws.Cells[1, columnNum]).VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
ws.get_Range(ws.Cells[1, 1], ws.Cells[1, columnNum]).Interior.Color = 0xf8819a;//单元格背景颜色
object[,] objheader = new object[rowNum + 1, columnNum];
int j = 0;
foreach (string header in headerName)
{
objheader[0, j] = header;
j++;
}
char mycolumns = (char)(columnNum + 64);
Excel.Range r = ws.get_Range("A1", mycolumns.ToString() + "1");
//xlApp.ActiveCell.HorizontalAlignment = Excel.Constants.xlCenter;
//xlApp.ActiveCell.Font.Bold = true;
r.Value2 = objheader;
for (int i = 0; i < rowNum; i++)
{
objheader[0, 0] = "";
objheader[0, 1] = FinishAccountDetailSummaryList[i].BuMen;
objheader[0, 2] = FinishAccountDetailSummaryList[i].PingZhengLeiXing;
objheader[0, 3] = FinishAccountDetailSummaryList[i].PingZhengRiQi;
objheader[0, 4] = FinishAccountDetailSummaryList[i].PingZhengHao;
objheader[0, 5] = FinishAccountDetailSummaryList[i].JinEr;
r = ws.get_Range("A" + (i + 2), mycolumns.ToString() + (i + 2));
xlApp.ActiveCell.Borders.Weight = 1;
r.Value2 = objheader;
}
r.EntireColumn.AutoFit();
xlBook.SaveCopyAs(strFileName);
}
catch (Exception ex)
{
throw ex;
}
finally
{
killexcel(xlApp);//调用杀进程的方法,如下:
GC.Collect();
}
View Code
[DllImport("user32.dll")]
private static extern int GetWindowThreadProcessId(IntPtr hwnd, out int id);
public void killexcel(Excel.Application xlapp)
{
try
{
IntPtr app = new IntPtr(xlapp.Hwnd);
int processid;
GetWindowThreadProcessId(app, out processid);
System.Diagnostics.Process.GetProcessById(processid).Kill();
}
catch
{ }
}
private static extern int GetWindowThreadProcessId(IntPtr hwnd, out int id);
public void killexcel(Excel.Application xlapp)
{
try
{
IntPtr app = new IntPtr(xlapp.Hwnd);
int processid;
GetWindowThreadProcessId(app, out processid);
System.Diagnostics.Process.GetProcessById(processid).Kill();
}
catch
{ }
}
posted on 2011-05-02 23:17 LeeXiaoLiang 阅读(343) 评论(0) 收藏 举报

浙公网安备 33010602011771号