在asp.net中操作excel,excel进程无法关闭解决方法(亲测有效)

这个问题纠缠我好几天了,终于在无数的解决方案中找到了有效的
具体实现如下:
[DllImport(“User32.dll”, CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
public static void Kill(Microsoft.Office.Interop.Excel.Application excel)
{
excel.Quit();
IntPtr t = new IntPtr(excel.Hwnd);
int k = 0;
GetWindowThreadProcessId(t, out k);
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
p.Kill();
}
在代码中直接kill(excel)即可

还有就是最好把Workbook eWorkbook = eExcel.Workbooks.Add(true);改写成两个对象,如下
Workbooks eWorkbooks = eExcel.Workbooks;
Workbook eWorkbook = eWorkbooks.Add(true);

下面是我的项目
生成excel文件,并将其转成PDF格式

string path1 = "D:\\" + EmpNo + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
string path2 = "D:\\" + EmpNo + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
string companyname = Microsoft.VisualBasic.Strings.StrConv(Convert.ToString(EmpnoDetail[0].ChComName), Microsoft.VisualBasic.VbStrConv.SimplifiedChinese, new System.Globalization.CultureInfo("zh-cn").LCID);
object missing = Type.Missing;
Microsoft.Office.Interop.Excel.Application eExcel = new Microsoft.Office.Interop.Excel.Application();
//原來的寫法
//Workbook eWorkbook = eExcel.Workbooks.Add(true);
Workbooks eWorkbooks = eExcel.Workbooks;
Workbook eWorkbook = eWorkbooks.Add(true);
Worksheet eWorksheet = (Worksheet)eWorkbook.Sheets.get_Item(1);
eWorksheet.PageSetup.TopMargin = 40.5;
eWorksheet.PageSetup.BottomMargin = 20;
eWorksheet.PageSetup.LeftMargin = 42.5;
eWorksheet.PageSetup.RightMargin = 30;
Range rangestart = eWorksheet.get_Range(eExcel.Cells[1, 1], eExcel.Cells[5, 4]);
rangestart.NumberFormat = "@";
rangestart.RowHeight = 20;
rangestart.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; //單元格設置左右居中
rangestart.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;//單元格設置上下居中
//下劃線                        rangestart.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
Range combine = eWorksheet.get_Range(eExcel.Cells[1, 1], eExcel.Cells[5, 1]);
combine.Merge(combine.MergeCells);//需合併單元格
combine.ColumnWidth = 4; //设置单元格的宽度
Range combine2 = eWorksheet.get_Range(eExcel.Cells[1, 2], eExcel.Cells[5, 2]);
combine2.ColumnWidth = 25; //设置单元格的宽度
Range combine24 = eWorksheet.get_Range(eExcel.Cells[4, 2], eExcel.Cells[4, 2]);
eExcel.Cells[4, 2] = "Wistron Corporation";
combine24.Font.Size = 9;
combine24.Font.Bold = true;
combine24.Font.ColorIndex = 1;
combine24.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignRight;
Range combine25 = eWorksheet.get_Range(eExcel.Cells[5, 2], eExcel.Cells[5, 2]);
eExcel.Cells[5, 2] = companyname;
combine25.Font.Size = 11;
combine25.Font.ColorIndex = 1;
combine25.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignRight;
Range combine3 = eWorksheet.get_Range(eExcel.Cells[1, 3], eExcel.Cells[5, 3]);
combine3.ColumnWidth = 23; //设置单元格的宽度
Range combine4 = eWorksheet.get_Range(eExcel.Cells[1, 4], eExcel.Cells[5, 4]);
combine4.ColumnWidth = 23; //设置单元格的宽度
Range combine31 = eWorksheet.get_Range(eExcel.Cells[1, 3], eExcel.Cells[5, 4]);
combine31.Merge(combine31.MergeCells);//需合併單元格
combine31.ColumnWidth = 23; //设置单元格的宽度
eExcel.Cells[1, 3] = "派遣结束证明书";
combine31.Font.Size = 18;
combine31.Font.Bold = true;
combine31.Font.ColorIndex = 1;
combine31.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; //單元格設置左右居中
combine31.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;//單元格設置上下居中
Range body0 = eWorksheet.get_Range(eExcel.Cells[6, 1], eExcel.Cells[6, 4]);
body0.Merge(body0.MergeCells);//需合併單元格
body0.RowHeight = 2; //设置单元格的宽度
Range body = eWorksheet.get_Range(eExcel.Cells[7, 1], eExcel.Cells[7, 4]);
body.Merge(body.MergeCells);//需合併單元格
body.RowHeight = 60;
string bodycontenet = "现有" + EmpnoDetail[0].Chname + ",自" + EmpnoDetail[0].HireYear + "年" + EmpnoDetail[0].HireMonth + "月" + EmpnoDetail[0].HireDay + "日至" + companyname + ",现由于工作派遣期满与" + companyname + EmpnoDetail[0].ResignYear + "年" + EmpnoDetail[0].ResignMonth + "月" + EmpnoDetail[0].ResignDay + "日结束工作";
eExcel.Cells[7, 1] = bodycontenet;
int flag = bodycontenet.IndexOf("工作派遣期满");
body.WrapText = true;
body.Characters[flag + 1, 6].Font.Bold = true;
Range body1 = eWorksheet.get_Range(eExcel.Cells[8, 1], eExcel.Cells[8, 4]);
body1.Merge(body1.MergeCells);//需合併單元格
body1.RowHeight = 20;
eExcel.Cells[8, 1] = "工号:" + EmpNo + " " + " 部门:" + EmpnoDetail[0].Dept + " " + "性别:" + EmpnoDetail[0].Sex;
Range END = eWorksheet.get_Range(eExcel.Cells[10, 4], eExcel.Cells[11, 4]);
END.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; //單元格設置左右居中
END.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;//單元格設置上下居中
eExcel.Cells[10, 4] = companyname;
eExcel.Cells[11, 4] = EmpnoDetail[0].NowYear + "年" + EmpnoDetail[0].NowMonth + "月" + EmpnoDetail[0].NowDay + "日";                            eWorksheet.Shapes.AddPicture("D:\\logo1.jpg", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, 0, 0, 20, 90);
Microsoft.Office.Core.MsoTriState.msoTrue, 285, 105, 300, 240);
eWorkbook.SaveAs(path1, missing, missing, missing, missing, missing, XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing, missing);
eExcel.Quit();
Kill(eExcel);                  Microsoft.Office.Interop.Excel.XlFixedFormatType xlTypePDF = XlFixedFormatType.xlTypePDF;//轉成pdf
Application applicationClass = new Application();
Workbook workbook = null;
string inputfileName = path1;
string outputFileName = path2;
XlFixedFormatType xlFixedFormatType = xlTypePDF;
XlFixedFormatQuality xlFixedFormatQuality = XlFixedFormatQuality.xlQualityStandard;
bool includeDocProperties = true;
bool openAfterPublish = false;
workbook = applicationClass.Workbooks.Open(inputfileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);

if (workbook != null)
{                               workbook.ExportAsFixedFormat(xlFixedFormatType, outputFileName, xlFixedFormatQuality, includeDocProperties, openAfterPublish, missing, missing, missing, missing);
}
workbook.Close(true, missing, missing);
applicationClass.Quit();
Kill(applicationClass);
FileStream fileStream = new FileStream(path2, FileMode.Open);
long fileSize = fileStream.Length;
byte[] fileBuffer = new byte[fileSize];
fileStream.Read(fileBuffer, 0, (int)fileSize);
fileStream.Close();
string[] filearray = System.IO.Directory.GetFiles("D:\\");
result = "";
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值