/// <summary> /// catches Excel's close event /// starts a Thread that send a ESC to the Excel window ;) /// </summary> private void OnClose(Excel.Workbook xls, ref bool cancel) { if (!deactivateevents) { cancel = true; } }
/// <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文件操作失败"); } }
/// <summary> /// 以excelOpenFileName为模板新建Excel文件 /// </summary> public bool OpenExcelFile() { if (xlsApp != null) { //检查文件是否存在 if (xlsOpenFileName == "") { throw new Exception("请选择文件!"); } } if (!File.Exists(xlsOpenFileName)) { throw new Exception(xlsOpenFileName + "该文件不存在!"); } try { xlsApp = new Excel.ApplicationClass(); xlsWbs = xlsApp.Workbooks; xlsWb = ((Excel.Workbook)xlsWbs.Open(xlsOpenFileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value)); xlsWs = (Excel.Worksheet)xlsWb.Worksheets[excelActiveWorkSheetIndex]; xlsApp.Visible = false; return(true); } catch (Exception e) { CloseExcelApplication(); throw new Exception("(1)没有安装Excel 2003;\n(2)或没有安装Excel 2003 .NET 可编程性支持;\n详细信息:\n" + e.Message); } }
//-------------------------------------------------------------------------------------------------------- /// <summary> /// 关闭Excel文件,释放对象;最后一定要调用此函数,否则会引起异常 /// </summary> /// <param></param> public void CloseExcelApplication() { try { xlsWbs = null; xlsWb = null; xlsWs = null; xlsRg = null; if (xlsApp != null) { xlsApp.ActiveWorkbook.Close(false, null, null); xlsApp.Workbooks.Close(); //Object missing = Type.Missing; xlsApp.Quit(); xlsApp = null; //ReleaseAllRef(excelApplication);//Error } } finally { GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } }
/// <summary> /// 保存 /// </summary> /// <param name="xlsSheet"></param> /// <param name="xlsBook"></param> /// <param name="xlsSheetName"></param> private string SaveAsExcel(Excel.Worksheet xlsSheet, Excel.Workbook xlsBook) { //string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) ; string desktopPath = @"C:\NPOI"; if (System.IO.Directory.Exists(desktopPath) == false) { System.IO.Directory.CreateDirectory(desktopPath); } desktopPath = desktopPath + "\\" + "IQC"; if (System.IO.Directory.Exists(desktopPath) == true) { System.IO.Directory.Delete(desktopPath); } xlsSheet.SaveAs(desktopPath, 56); //保存在 ////OpenXls(desktopPath); //PrinttheExcel(xlsSheet, desktopPath); xlsBook.Close(); xlsSheet = null; KillProcess("Excel"); string dd = System.IO.Path.GetExtension(desktopPath); return(desktopPath + ".xls"); }
/// <summary> /// 关闭Excel /// </summary> public void Close() { myWorkBook.Close(Type.Missing, Type.Missing, Type.Missing); myExcel.Quit(); myWorkBook = null; myExcel = null; GC.Collect(); }
/// <summary> /// 关闭Excel /// </summary> /// <param name="isSave">是否保存</param> /// <param name="fileName">存储文件名</param> public void Close(bool isSave, string fileName) { myWorkBook.Close(isSave, fileName, Type.Missing); myExcel.Quit(); myWorkBook = null; myExcel = null; GC.Collect(); }
public void newWorkbook(string excelTemplate, string fileName) { //以excelTemplate为模板新建文件fileName //excelApplication. xlsWb = xlsWbs.Add(excelTemplate); SaveFileName = ""; SaveAsExcel(false); }
public ClsAllExcel() { xlsApp = null; xlsWbs = null; xlsWb = null; xlsWs = null; ActiveSheetIndex = 1; }
public static void doit(Excel.Application app, ArrayList excelnames, string command) { ArrayList prods = new ArrayList(); ArrayList words = new ArrayList(); Hashtable nts = new Hashtable(); foreach (string excelname in excelnames) { Excel.Workbook wb = app.Workbooks.Open( excelname, // Filename 0, // UpdateLinks true, // ReadOnly 5, // Format "", // Password "", // WriteResPassword true, // IgnoreReadOnlyRecommended Excel.XlPlatform.xlWindows, // Origin "\t", // Delimiter false, // Editable false, // Notify 0, // Converter true, // AddToMru true, // Local true // CorruptLoad ); prods = process(wb, prods); nts = processTypes(wb, nts); words = processKeyword(wb, words); } switch (command) { default: System.Console.WriteLine("usage: a.out (grammar|keywords) excelfile"); break; case "gram": prods = addOpts(prods, nts); foreach (production p in prods) { System.Console.WriteLine(p.prod); } break; case "rewrite": prods = addOpts(prods, nts); emitParse2AST(nts, prods); break; case "keywords": foreach (string s in words) { System.Console.WriteLine(s); } break; } }
//Ribbon界面的回调函数,响应事件,将Excel表格数据写入CAD public void OnButton1Pressed(IRibbonControl control) { //选取Excel表格数据 wbk = app.ActiveWorkbook; wsh = (Worksheet)wbk.ActiveSheet; Excel.Range rngLeftTop = null; Excel.Range rngRightButtom = null; rngLeftTop = (Excel.Range)app.InputBox("点击左上角单元格", Type: 8); rngRightButtom = (Excel.Range)app.InputBox("点击右下角单元格", Type: 8); object[,] data = (object[, ])wsh.Range[rngLeftTop.Address + ":" + rngRightButtom.Address].Value2; //把数据导入Access数据库 try { AccessDataBase.WriteDB(data); MessageBox.Show("选定的数据读取完毕,点击确定导入CAD"); } catch (SystemException ex) { MessageBox.Show(ex.ToString()); } //选定的数据读取完毕,点击确定导入CAD if (rngLeftTop != null & rngRightButtom != null) { //打开cad try { AcadApp = (Autodesk.AutoCAD.Interop.AcadApplication)System.Runtime.InteropServices.Marshal.GetActiveObject("AutoCAD.Application"); AcadDoc = AcadApp.ActiveDocument; } catch { OpenFileDialog op = new OpenFileDialog(); op.Filter = "CAD文件(*.dwg)|*.dwg|CAD图形文件(*.dxf)|*.dxf"; op.Title = "打开CAD文件"; op.ShowDialog(); string filePath = op.FileName; if (filePath == "") { MessageBox.Show("选择CAD文件无效!!", "文件无效!!"); } else { AcadApp = new Autodesk.AutoCAD.Interop.AcadApplication(); AcadDoc = AcadApp.Documents.Open(filePath, null, null); } } AcadApp.Application.Visible = true; //使CAD程序跳到在最前面,需要添加引用“Microsoft.VisualBasic” Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption); //让CAD自动执行netload命令加载程序集DLL,如果注册表加载方法无效的话 AcadDoc.SendCommand("(command \"_netload\" \"" + @"C:\\Windows\\System\\xl2cad\\xl2cad.dll" + "\") "); AcadDoc.SendCommand("zdbtext "); } else { MessageBox.Show("没有选择数据!"); } }
public void Open(string FileName)//打开一个Excel文件 { app = new Excel.Application(); wbs = app.Workbooks; wb = wbs.Add(FileName); //wb = wbs.Open(FileName, 0, true, 5,"", "", true, Excel.XlPlatform.xlWindows, "t", false, false, 0, true,Type.Missing,Type.Missing); //wb = wbs.Open(FileName,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Excel.XlPlatform.xlWindows,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing); mFilename = FileName; }
public void CreateExcel(IDictionary <string, System.Data.DataTable> iDicDT, string FilePath, bool Visible) { Excel.Application excel = new Excel.Application(); //引用Excel对象 Excel.Workbook book = excel.Application.Workbooks.Add(System.Reflection.Missing.Value); //引用 Excel.Worksheet sheet3 = (Excel.Worksheet)book.Worksheets["Sheet3"];//选中 object nothing = System.Reflection.Missing.Value; book.Worksheets.Add(nothing, sheet3, iDicDT.Count - 3, nothing); int index = 0; foreach (KeyValuePair <string, System.Data.DataTable> val in iDicDT) { index++; Excel.Worksheet tempExcel = (Excel.Worksheet)book.Worksheets["Sheet" + index];//选中 tempExcel.Name = val.Key; System.Data.DataTable tempdt = val.Value; for (int i = 0; i < tempdt.Columns.Count; i++) { tempExcel.Cells[1, i + 1] = tempdt.Columns[i].ColumnName; } for (int i = 0; i < tempdt.Rows.Count; i++) { for (int j = 0; j < tempdt.Columns.Count; j++) { try { tempExcel.Cells[i + 2, j + 1] = tempdt.Rows[i][j].ToString(); } catch { } } } } //保存 if (!string.IsNullOrEmpty(FilePath)) { SaveExcelFile(excel, FilePath); } //打开显示 excel.Visible = Visible; excel.Quit(); excel = null; GC.Collect();//垃圾回收 }
/// <summary> /// catches Excel's newworkbook event /// just close /// </summary> private void OnNewXls(Excel.Workbook xls) { if (!deactivateevents) { deactivateevents = true; object dummy = null; ((Excel._Workbook)workbook).Close(dummy, dummy, dummy); deactivateevents = false; } }
//关闭一个Excel对象,销毁对象 public void Close() { //wb.Save(); wb.Close(Type.Missing, Type.Missing, Type.Missing); wbs.Close(); app.Quit(); wb = null; wbs = null; app = null; GC.Collect(); }
private void createExcel() { Excel.Application excelApp = null; Excel.Workbook workbook = null; Excel.Sheets sheets = null; Excel.Worksheet newSheet = null; try { FileInfo file = new FileInfo(fileLoc); if (file.Exists) { excelApp = new Excel.Application(); workbook = excelApp.Workbooks.Open(fileLoc, 0, false, 5, "", "", false, XlPlatform.xlWindows, "", true, false, 0, true, false, false); sheets = workbook.Sheets; //check columns exist foreach (Excel.Worksheet sheet in sheets) { Console.WriteLine(sheet.Name); sheet.Select(Type.Missing); System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet); } newSheet = (Worksheet)sheets.Add(sheets[1], Type.Missing, Type.Missing, Type.Missing); newSheet.Name = "My New Sheet"; newSheet.Cells[1, 1] = "BOO!"; workbook.Save(); workbook.Close(null, null, null); excelApp.Quit(); } } finally { System.Runtime.InteropServices.Marshal.ReleaseComObject(newSheet); System.Runtime.InteropServices.Marshal.ReleaseComObject(sheets); System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp); newSheet = null; sheets = null; workbook = null; excelApp = null; GC.Collect(); } }
public static void Merge(string sourceFile, string baseFile, string targetFile, int rowCount, int columnCount) { object MISSING = Type.Missing; Excel.Application excel = new Excel.Application(); try { Excel.Workbook sourceBook = excel.Workbooks.Open(sourceFile, MISSING, MISSING, MISSING, MISSING, MISSING, MISSING, MISSING, MISSING, MISSING, MISSING, MISSING, MISSING, MISSING, MISSING); Excel.Worksheet sourceSheet = (Excel.Worksheet)sourceBook.Sheets[1]; Excel.Workbook baseBook = excel.Workbooks.Open(baseFile, MISSING, MISSING, MISSING, MISSING, MISSING, MISSING, MISSING, MISSING, MISSING, MISSING, MISSING, MISSING, MISSING, MISSING); Excel.Worksheet baseSheet = (Excel.Worksheet)baseBook.Sheets[1]; //baseSheet.Unprotect(string.Empty); for (int column = 1; column <= columnCount; column++) { for (int row = 1; row <= rowCount; row++) { Excel.Range baseRange = (Excel.Range)baseSheet.Cells[row, column]; if (baseRange.AllowEdit) { Excel.Range sourceRange = (Excel.Range)sourceSheet.Cells[row, column]; baseRange.Value2 = sourceRange.Value2; } } } //baseSheet.Protect(string.Empty, // MISSING, MISSING, MISSING, MISSING, MISSING, MISSING, MISSING, // MISSING, MISSING, MISSING, MISSING, MISSING, MISSING, MISSING, MISSING); sourceBook.Close(false, MISSING, MISSING); baseBook.SaveCopyAs(targetFile); baseBook.Close(false, MISSING, MISSING); } finally { Office.ExcelHelper.Kill(excel); } Thread.Sleep(100); }
/// <summary> /// 打开Excel /// </summary> /// <param name="printIQCDataXlsPath"></param> /// <returns></returns> private static Excel.Workbook OpenTheExcel(string printIQCDataXlsPath) { string fileName = printIQCDataXlsPath; if (printIQCDataXlsPath == string.Empty) { fileName = @"\\192.168.0.237\LightMasterSpc\lmSpc\System\ProductSizeSpecPicture\品保课\IQC.xls"; } Excel.Application xlsApp = new Excel.Application(); Excel.Workbook xlsBook = xlsApp.Workbooks.Add(fileName); xlsApp.DisplayAlerts = false; xlsApp.Visible = false; return(xlsBook); }
/// <summary> /// 创建一个Excel示例 /// </summary> public void CreateExcel2() { Excel.Application excel = new Excel.Application(); //引用Excel对象 Excel.Workbook book = excel.Application.Workbooks.Add(System.Reflection.Missing.Value); //引用 Excel.Worksheet sheet = (Excel.Worksheet)book.Worksheets["Sheet1"]; //选中 sheet.Cells[1, 3] = "Mytest"; //左上角第一格的坐标是[1,1] Excel.Worksheet sheet2 = (Excel.Worksheet)book.Worksheets["Sheet2"]; //选中 sheet2.Cells[1, 3] = "Mytestdddd"; //左上角第一格的坐标是[1,1] book.Worksheets.Add(After: sheet2, Count: 5); excel.Visible = true;//使Excel可视 //保存 switch (ExistsRegedit()) { case 1: { excel.ActiveWorkbook.SaveAs("D:\\test.xls", XlFileFormat.xlExcel9795, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null); break; } case 2: { excel.ActiveWorkbook.SaveAs("D:\\test.xls", Excel.XlFileFormat.xlExcel7, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null); break; } default: throw new Exception("您的电脑需要安装Office2003 或 Office2007才能导出Excel文件。"); } //打开显示 excel.Visible = true; // excel.Quit(); // excel=null; // GC.Collect();//垃圾回收 }
/// <summary> /// 导出gridview为EXCEL /// </summary> /// <param name="GridView1"></param> public void Export(GridView GridView1) { //Excel.ApplicationClass oExcel = new Excel.ApplicationClass(); Excel.ApplicationClass oExcel = new Excel.ApplicationClass(); object oMissing = System.Reflection.Missing.Value; oExcel.Workbooks.Add(oMissing); Excel.Workbook oBook = oExcel.Workbooks[1]; Excel.Worksheet oSheet = (Excel.Worksheet)oBook.Sheets[1]; oSheet.Name = "最终名单";//this.Title; Excel.Range rg; //String test = GridView1.Rows[0].Cells[0].Text; for (int j = 0; j < GridView1.HeaderRow.Cells.Count; j++) { rg = ((Excel.Range)oSheet.Cells[1, j + 1]); rg.FormulaR1C1 = GridView1.HeaderRow.Cells[j].Text; } for (int i = 0; i < GridView1.Rows.Count; i++) { for (int j = 0; j < GridView1.Rows[0].Cells.Count; j++) { rg = ((Excel.Range)oSheet.Cells[i + 2, j + 1]); rg.NumberFormatLocal = "@";//设置单元格格式为文本 rg.FormulaR1C1 = GridView1.Rows[i].Cells[j].Text; } } rg = null; string VirFileName = Guid.NewGuid().ToString() + ".xls"; oBook.SaveAs(Server.MapPath(VirFileName), Excel.XlFileFormat.xlExcel9795, oMissing, oMissing, oMissing, oMissing, Excel.XlSaveAsAccessMode.xlExclusive, oMissing, oMissing, oMissing, oMissing, oMissing); oExcel.Workbooks.Close(); oExcel.Quit(); oSheet = null; oBook = null; oExcel = null; GC.Collect(); Response.Redirect(VirFileName); }
private Excel.ApplicationClass Open(string xlsFileName) { try { Excel.ApplicationClass excelCls = new ApplicationClass(); Excel.Workbook excelWb = excelCls.Workbooks.Add(xlsFileName); return(excelCls); } catch (Exception ex) { MessageBox.Show( ex.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error ); return(null); } }
static ArrayList processKeyword(Excel.Workbook wb, ArrayList words) { foreach (Excel.Worksheet ws in wb.Sheets) { if (ws.Name.EndsWith("keyword-tokens")) { Excel.Range keywords = ws.get_Range("tokens_Terminal", missing); for (int i = 1; i <= ws.UsedRange.Rows.Count; i++) { string token = item(keywords, i); if (token != "") { words.Add(token); } } } } return(words); }
public static System.Data.DataTable LoadDataFromExcel(string filePath) //从指定文件路径中的Excel读取所有内容到System.Data.DataTable(xls和xlsx格式均可以打开) { Excel.Application excel = new Excel.Application(); //对象实例化; try { Excel.Workbook workbook = excel.Workbooks.Open(filePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); Excel.Worksheet worksheet = (Worksheet)workbook.Worksheets[1]; excel.Visible = false; //不显示Excel内容 int rowCount = worksheet.UsedRange.Rows.Count; //定义Excel文件中的行数 int colCount = worksheet.UsedRange.Columns.Count; //定义Excel文件中的列数 Excel.Range range; System.Data.DataTable dt = new System.Data.DataTable(); for (int i = 0; i < colCount; i++)//循环列 { range = (Excel.Range)excel.Cells[1, i + 1]; dt.Columns.Add(range.Value2.ToString()); } for (int j = 1; j < rowCount; j++)//循环行 { DataRow dr = dt.NewRow(); for (int i = 0; i < colCount; i++) { range = (Excel.Range)excel.Cells[j + 1, i + 1]; dr[i] = range.Value2.ToString(); } dt.Rows.Add(dr); } excel.DisplayAlerts = false; //设置禁止弹出保存和覆盖的询问提示框 excel.AlertBeforeOverwriting = false; excel.Workbooks.Close(); //关闭工作簿 excel.Quit(); //退出Excel程序 return(dt); } catch (Exception ex) { //throw new Exception(ex.Message); //MessageBox.Show(ex.Message); return(null); } }
public static void Main() { Excel.Application ex = new Excel.Application(); ex.Visible = true; Excel.Workbook wb = ex.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet); Excel.Range rg = ex.ActiveCell; rg.set_Item(1, 1, "Competência"); rg.set_Item(1, 2, "Nota"); rg.set_Item(2, 1, "C++"); rg.set_Item(2, 2, "5"); rg.set_Item(3, 1, "UML"); rg.set_Item(3, 2, "6"); rg.set_Item(4, 1, "OOP"); rg.set_Item(4, 2, "5"); rg.set_Item(5, 1, "DB"); rg.set_Item(5, 2, "9"); rg.set_Item(6, 1, ".NET"); rg.set_Item(6, 2, "4"); rg.set_Item(7, 1, "XML"); rg.set_Item(7, 2, "4"); rg.AutoFormat(Excel.XlRangeAutoFormat.xlRangeAutoFormatClassic3, true, true, true, true, true, true); Excel.Chart ch = (Excel.Chart)ex.Charts.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing); ch.ChartType = Excel.XlChartType.xlRadarFilled; Excel.Worksheet ws = (Excel.Worksheet)ex.Worksheets["Plan1"]; Excel.Range lrg = ws.get_Range("A1", "B7"); ch.SetSourceData(lrg, Excel.XlRowCol.xlColumns); ch.Location(Excel.XlChartLocation.xlLocationAsObject, "Plan1"); ex.ActiveChart.HasLegend = false; ex.ActiveChart.HasTitle = true; ex.ActiveChart.ChartTitle.Text = "Gráfico de Competência"; Console.Write("Pressionar ENTER para sair do EXCEL"); Console.ReadLine(); ex.Quit(); }
/// <summary> /// /// </summary> /// <returns></returns> private Excel.ApplicationClass OpenExcel() { try { string xlsFileName = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\report\\rptWcMonth.xls"; Excel.ApplicationClass excelCls = new ApplicationClass(); Excel.Workbook excelWb = excelCls.Workbooks.Add(xlsFileName); return(excelCls); } catch (Exception ex) { MsgBox.Show( ex.ToString(), "错误", MessageBoxIcon.Error ); return(null); } }
private void ExportExcel(DataTable dt) { if (dt == null) { return; } Excel.Application xlApp = new Excel.Application(); if (xlApp == null) { return; } System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture; System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); Excel.Workbooks workbooks = xlApp.Workbooks; Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet); Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1]; Excel.Range range; long totalCount = dt.Rows.Count; long rowRead = 0; float percent = 0; for (int i = 0; i < dt.Columns.Count; i++) { worksheet.Cells[1, i + 1] = transColumnName(dt.Columns[i].ColumnName); range = (Excel.Range)worksheet.Cells[1, i + 1]; range.Interior.ColorIndex = 15; range.Font.Bold = true; } for (int r = 0; r < dt.Rows.Count; r++) { for (int i = 0; i < dt.Columns.Count; i++) { worksheet.Cells[r + 2, i + 1] = dt.Rows[r][i]; } rowRead++; percent = ((float)(100 * rowRead)) / totalCount; } xlApp.Visible = true; }
static ArrayList process(Excel.Workbook wb, ArrayList prods) { foreach (Excel.Worksheet ws in wb.Sheets) { if (ws.Name.EndsWith("grammar")) { Excel.Range nonterms = ws.get_Range("grammar_Nonterminal", missing); Excel.Range rules = ws.get_Range("grammar_Rule", missing); Excel.Range types = ws.get_Range("grammar_type", missing); Excel.Range actions = ws.get_Range("grammar_Action", missing); for (int i = 1; i <= ws.UsedRange.Rows.Count; i++) { string nt = item(nonterms, i); string rule = item(rules, i); string ty = item(types, i); string action = item(actions, i); if (action == "") { if (rule == "") { action = "null"; } else { action = "a1"; } } if (nt != "") { string[] rx = rule.Trim(' ').Split(' '); production p = new production(nt, rx, ty, action); prods.Add(p); } } } } return(prods); }
static Hashtable processTypes(Excel.Workbook wb, Hashtable T) { foreach (Excel.Worksheet ws in wb.Sheets) { if (ws.Name.EndsWith("types")) { Excel.Range nonterms = ws.get_Range("types_Nonterminal", missing); Excel.Range types = ws.get_Range("types_type", missing); Excel.Range actions = ws.get_Range("types_Default_Action", missing); for (int i = 1; i <= ws.UsedRange.Rows.Count; i++) { string nt = item(nonterms, i); string ty = item(types, i); string action = item(actions, i); if (nt != "") { T[nt] = new typeInfo(ty, action); } } } } return(T); }
private void button1_Click(object sender, EventArgs e) { saveFileDialog1.Title = "序列号生成工具----百思威科技"; saveFileDialog1.Filter = "Excel(*.xls)|*.xls"; saveFileDialog1.FileName = string.Format("序列号_{0}", DateTime.Now.ToString("yyyyMMdd")); DialogResult result = saveFileDialog1.ShowDialog(); Excel._Application xlapp = new Excel.Application(); Excel.Workbook xlbook = xlapp.Workbooks.Add(true); Excel.Worksheet xlsheet = (Excel.Worksheet)xlbook.Worksheets[1]; int RowCount = Convert.ToInt32(len.Text); int RowIndex = 0; for (int i = 0; i < RowCount; i++) { RowIndex++; xlsheet.Cells[RowIndex, 1] = "SN:" + (Convert.ToInt32(sarial.Text) + i).ToString(); } xlbook.Saved = true; xlbook.SaveCopyAs(saveFileDialog1.FileName); xlapp.Quit(); MessageBox.Show("导出成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); }
//字母-〉列号 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); } }