保存Excel文件示例:
支持:Office2003的xls,和Office2007的xlsx格式!
// 需要引用:Microsoft.Office.Interop.Excel.dll 10.0版本以上,支持Office2003. // 需要引用:Microsoft.Office.Interop.Excel.dll 12.0版本的支持Office2007.
using Microsoft.Office.Interop.Excel; using System.Runtime.InteropServices; using System.Windows;
namespace SaveExcelSample { public class ExcelHelper {
public static void SaveToExcel2003(string SaveFile) { // 需要引用:Microsoft.Office.Interop.Excel.dll 10.0版本以上,支持Office2003. Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel._Workbook workBook= excel.Workbooks.Add(); Microsoft.Office.Interop.Excel._Worksheet workSheet = (Microsoft.Office.Interop.Excel._Worksheet)workBook.ActiveSheet; object misValue = System.Reflection.Missing.Value;
int rowIndex = 1; int colIndex = 0;
//取得标题 for (int i = 0; i < 5; i++) { colIndex++; excel.Cells[1, colIndex] = "标题" + i; }
//取得表格中的数据 for (int i = 0; i < 3; i++) { rowIndex++; colIndex = 0;
for (int j = 0; j < 5; j++) { colIndex++; excel.Cells[rowIndex, colIndex] = i + j;
//设置表格内容居中对齐 //workSheet.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter; } }
excel.Visible = false; // 不显示Excel窗口
workBook.SaveAs(SaveFile, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); workBook.Close(true, misValue, misValue); excel.Quit();
PublicMethod.Kill(excel);//调用kill当前excel进程 releaseObject(workSheet);//释放COM对象 releaseObject(workBook); releaseObject(excel);
GC.Collect(); }
public static void SaveToExcel2007(string filePath) { // 创建Excel文档,保存格式 Office2007 xlsx. // 需要引用:Microsoft.Office.Interop.Excel.dll 12.0版本的支持Office2007. Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook workBook = excel.Workbooks.Add(); Microsoft.Office.Interop.Excel.Worksheet workSheet = workBook.Worksheets.Add();
//excel.DisplayAlerts = false; // 不显示告警
int rowIndex = 1; int colIndex = 0;
//取得标题 for (int i = 0; i < 5; i++) { colIndex++; workSheet.Cells[1, colIndex] = "标题" + i; }
//取得表格中的数据 for (int i = 0; i < 3; i++) { rowIndex++; colIndex = 0;
for (int j = 0; j < 5; j++) { colIndex++; workSheet.Cells[rowIndex, colIndex] = i + j; } }
//文件保存 workSheet.SaveAs(filePath); workBook.Close(); excel.Quit();
PublicMethod.Kill(excel);//调用kill当前excel进程 releaseObject(workSheet);//释放COM对象 releaseObject(workBook); releaseObject(excel);
GC.Collect(); //MessageBox.Show(string.Format("{0} 文档生成成功.", filePath)); }
private static void releaseObject(object obj) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null; } catch { obj = null; } }
public class PublicMethod { [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) { try { IntPtr ptr = new IntPtr(excel.Hwnd); int processID = 0;
GetWindowThreadProcessId(ptr, out processID); System.Diagnostics.Process process = System.Diagnostics.Process.GetProcessById(processID); process.Kill(); } catch (Exception ex) { Console.WriteLine(" ==== " + ex.Message); } } } } }
注:以上来源互联网,并整理。
|
请发表评论