/// <summary> /// Excel文件打印预览 /// </summary> /// <param name="ExcelFile">文件路径包含文件名称</param> public static void ExcelPreview(string ExcelFile) { try { Excel.Application xlsApp = new Excel.Application(); if (xlsApp == null) { throw new Exception("无法创建Excel对象,可能您的计算机未安装Excel"); } Excel.Workbooks xlsWbs = xlsApp.Workbooks; Excel.Workbook xlsWb = xlsWbs.Open( ExcelFile, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); xlsApp.Visible = true; xlsWb.PrintPreview(false); xlsWb = null; xlsApp.Quit(); xlsApp = null; } catch (Exception ex) { throw new Exception(ex.Message + "Excel文件操作失败"); } }
//字母-〉列号 Z-〉26 //public void SaveAsExcel() //{ // if (xlsSaveFileName == "") // { // throw new Exception("未指定要保存的文件名"); // } // try // { // xlsApp.Visible = true; // xlsWb.PrintPreview(false); // xlsWb = null; // xlsApp.Quit(); // xlsApp = null; // //xlsWs.SaveAs(xlsSaveFileName, Excel.XlFileFormat.xlExcel7, // // Type.Missing, Type.Missing, // // Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, // // Type.Missing, Type.Missing); // } // catch (Exception e) // { // CloseExcelApplication(); // throw new Exception(e.Message); // } //} public void SaveAsExcel(bool IsView) { try { if (File.Exists(xlsSaveFileName)) { File.Delete(xlsSaveFileName); } if (IsView) { xlsApp.Visible = true; xlsWb.PrintPreview(false); } xlsWb.Close(true, xlsSaveFileName, Type.Missing); xlsWb = null; xlsApp.Quit(); xlsApp = null; } catch (Exception e) { CloseExcelApplication(); throw new Exception(e.Message); } }