C#导出到Excel模板中,保存后Excel进程无法结束
int rowCount = dt.Rows.Count;//源DataTable行数
int colCount = (dt.Columns.Count - 9);//源DataTable列数减一,最后一列为项目类型名称,用于标题的
int sheetCount = this.GetSheetCount(rowCount, rows);//WorkSheet个数,即页数
DateTime beforeTime;
DateTime afterTime;
if (sheetPrefixName == null || sheetPrefixName.Trim() == "")
sheetPrefixName = "Sheet";
//创建一个Application对象并使其可见
beforeTime = DateTime.Now;
Excel.Application app = new Excel.ApplicationClass();
app.Visible = true;
afterTime = DateTime.Now;
//打开模板文件,得到WorkBook对象
Excel.Workbook workBook = app.Workbooks._Open(templetFile, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing);
//得到一个WorkSheet对象
Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(1);
//复制sheetCount-1个WorkSheet对象
for (int i = 1; i < sheetCount; i++)
{
((Excel.Worksheet)workBook.Worksheets.get_Item(i)).Copy(missing, workBook.Worksheets[i]);
}
//将源DataTable数据写入Excel
for (int i = 1; i <= sheetCount; i++)
{
int startRow = (i - 1) * rows;//记录起始行索引
int endRow = i * rows;//记录结束行索引
//若是最后一个WorkSheet,那么记录结束行索引为源DataTable行数
if (i == sheetCount)
endRow = rowCount;
//获取要写入数据的WorkSheet对象,并重命名
Excel.Worksheet sheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
sheet.Name = sheetPrefixName + "-" + i.ToString();
//将dt中的数据写入WorkSheet
for (int j = 0; j < endRow - startRow; j++)
{
int z = startRow + j + 1;
sheet.Cells[top + j, left] = z.ToString();//第一列为检索结果的序号
for (int k = 1; k < colCount + 1; k++)
{
sheet.Cells[top + j, left + k] = dt.Rows[startRow + j][k].ToString();
}
}
}
//输出Excel文件并退出
try
{
workBook.SaveAs(outputFile, missing, missing, missing, missing, missing, Excel.XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing, missing);
workBook.Close(null, null, null);
app.Workbooks.Close();
app.Application.Quit();
app.Quit();
}
catch (Exception e)
{
throw e;
}
finally
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
workSheet = null;
workBook = null;
app = null;
GC.Collect();
}
[解决办法]
public static void ExportToExcel(string[,] exportData1, string[,] exportData2, string exportFile, string templateFile, string templetFilePath) { Excel._Application app; Excel.Workbook workBook; Excel.Worksheet workSheet; Excel.Range range; app = new Excel.ApplicationClass(); app.Visible = false; workBook = app.Workbooks.Open(templetFilePath + templateFile, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(2); // Read parameters from the second worksheet of xls. int datasheet = Convert.ToInt32((workSheet.get_Range("B1", Missing.Value)).Value2); int startRow1 = Convert.ToInt32((workSheet.get_Range("B2", Missing.Value)).Value2); int startCol1 = Convert.ToInt32((workSheet.get_Range("B3", Missing.Value)).Value2); int startRow2 = Convert.ToInt32((workSheet.get_Range("B4", Missing.Value)).Value2); int startCol2 = Convert.ToInt32((workSheet.get_Range("B5", Missing.Value)).Value2); workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(datasheet); int rowCount = exportData1.GetLength(0); int colCount = exportData1.GetLength(1); range = (Excel.Range)workSheet.Cells[startRow1, startCol1]; //写入Excel的坐标 range = range.get_Resize(rowCount, colCount); range.Value2 = exportData1; rowCount = exportData2.GetLength(0); colCount = exportData2.GetLength(1); range = (Excel.Range)workSheet.Cells[startRow2, startCol2]; //写入Excel的坐标 range = range.get_Resize(rowCount, colCount); range.Value2 = exportData2; workBook.SaveAs(templetFilePath + exportFile, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); workBook.Close(null, null, null); app.Workbooks.Close(); app.Quit(); if (range != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(range); range = null; } if (workSheet != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet); workSheet = null; } if (workBook != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook); workBook = null; } if (app != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(app); app = null; } GC.Collect(); DownLoadFile(templetFilePath, exportFile); DeleteFile(templetFilePath + exportFile); }
[解决办法]
先判断App是否 已存在
[解决办法]
学习中,顶
[解决办法]
你可以KIll掉他啊,保存玩了先Kill掉他啊
[解决办法]
单步调试试试!!!
看看问题所在!!!
[解决办法]
还是用存储过程导出吧,这个没什么限制,在服务器端不用装什么控件
zjx的proc
create strS