/// <summary> /// 加载数据同时保存数据到指定位置 /// </summary> /// <param name="obj"></param> private void AddData(FarPoint.Win.Spread.FpSpread obj) { wait = new WaitDialogForm("", "正在加载数据, 请稍候..."); try { //打开Excel表格 //清空工作表 fpSpread1.Sheets.Clear(); obj.OpenExcel(System.Windows.Forms.Application.StartupPath + "\\xls\\中压配电网分析.xls"); PF.SpreadRemoveEmptyCells(obj); //this.AddCellChanged(); //this.barEditItem2.Visibility = DevExpress.XtraBars.BarItemVisibility.Never; //S4_2_1.AddBarEditItems(this.barEditItem2, this.barEditItem1, this); } catch (System.Exception e) { //如果打开出错则重新生成并保存 LoadData(); //判断文件夹是否存在,不存在则创建 if (!Directory.Exists(System.Windows.Forms.Application.StartupPath + "\\xls")) { Directory.CreateDirectory(System.Windows.Forms.Application.StartupPath + "\\xls"); } //保存EXcel文件 obj.SaveExcel(System.Windows.Forms.Application.StartupPath + "\\xls\\中压配电网分析.xls", FarPoint.Excel.ExcelSaveFlags.NoFlagsSet); // 定义要使用的Excel 组件接口 // 定义Application 对象,此对象表示整个Excel 程序 Microsoft.Office.Interop.Excel.Application excelApp = null; // 定义Workbook对象,此对象代表工作薄 Microsoft.Office.Interop.Excel.Workbook workBook; // 定义Worksheet 对象,此对象表示Execel 中的一张工作表 Microsoft.Office.Interop.Excel.Worksheet ws = null; Microsoft.Office.Interop.Excel.Range range = null; excelApp = new Microsoft.Office.Interop.Excel.Application(); string filename = System.Windows.Forms.Application.StartupPath + "\\xls\\中压配电网分析.xls"; workBook = excelApp.Workbooks.Open(filename, 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); for (int i = 1; i <= workBook.Worksheets.Count; i++) { ws = (Microsoft.Office.Interop.Excel.Worksheet)workBook.Worksheets[i]; //取消保护工作表 ws.Unprotect(Missing.Value); //有数据的行数 int row = ws.UsedRange.Rows.Count; //有数据的列数 int col = ws.UsedRange.Columns.Count; //创建一个区域 range = ws.get_Range(ws.Cells[1, 1], ws.Cells[row, col]); //设区域内的单元格自动换行 range.WrapText = true; //保护工作表 ws.Protect(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, Missing.Value, Missing.Value); } //保存工作簿 workBook.Save(); //关闭工作簿 excelApp.Workbooks.Close(); } wait.Close(); }
public bool InsertFpSpread(FarPoint.Win.Spread.FpSpread fp, int sheetcount, TONLI.BZH.UI.DSOFramerWordControl wordcontrol) { try { string filename = System.Windows.Forms.Application.StartupPath + "\\BlogData\\Blog.xls"; fp.SaveExcel(filename); if (sheetcount == 0) sheetcount = fp.Sheets.Count; //fp.ActiveSheetIndex = 0; //for (int j = 9; j > 0; j++) //{ // fp.Sheets[j].Dispose(); //} //fp.Sheets[0].SaveTextFile("" //FarPoint.Win.Spread.FpSpread ft=fp; //for (int i = 0; i < sheetcount; i++) //{ // FarPoint.Win.Spread.FpSpread ft = fp; // for (int j = sheetcount; j > i; j--) // { // ft.Sheets.RemoveAt(j); // } // ft.SaveExcel(filename); // wordcontrol.DoInsertOleObject(filename); //} //for (int j = 6; j > 1; j--) //{ // ft.Sheets.RemoveAt(j); // ft.SaveExcel(filename); // wordcontrol.DoInsertOleObject(filename); //} //string filename = System.Windows.Forms.Application.StartupPath + "\\BlogData\\Blog.xls"; //string filename1 = System.Windows.Forms.Application.StartupPath + "\\BlogData\\Blog1.xls"; //fp.SaveExcel(filename); //FarPoint.Win.Spread.FpSpread fs = new FarPoint.Win.Spread.FpSpread(); //fs.Sheets[0].OpenExcel(filename, 1); //fs.SaveExcel(filename1); //wordcontrol.DoInsertOleObject(filename1); //FarPoint.Win.Spread.FpSpread fs = new FarPoint.Win.Spread.FpSpread(); //for (int i = 0; i < sheetcount; i++) //{ // try // { // fs.Sheets.Clear(); // fs.Sheets.Add(fp.Sheets[i]); // fs.SaveExcel(filename); //for (int i = 0; i < sheetcount; i++) //{ // fp.ActiveSheetIndex = i; // MessageBox.Show("111"); // fp.SaveExcel(filename); // MessageBox.Show("222"); // wordcontrol.DoInsertOleObject(filename); // MessageBox.Show("333"); //} //} //catch { } //} System.Drawing.Font font = new System.Drawing.Font("���ו", 12); Microsoft.Office.Interop.Excel.Application ep = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel._Workbook wb = ep.Workbooks.Add(filename); Microsoft.Office.Interop.Excel.Sheets sheets = wb.Worksheets; for (int i = 1; i <= sheetcount; i++) { //wordcontrol.DoInsertOleObject(filename); Microsoft.Office.Interop.Excel._Worksheet ws = (Microsoft.Office.Interop.Excel._Worksheet)sheets.get_Item(i);// [System.Type.Missing];//.get.get_Item("xx"); // ws.UsedRange.se.UsedRange.Select(); //cli ws.UsedRange.Copy(System.Type.Missing); wordcontrol.DoPaste(); wordcontrol.DoInsert(" ", font, TONLI.BZH.UI.WdParagraphAlignment.Left); } Clipboard.Clear(); ep.DisplayAlerts = false; ep.Quit(); return true; } catch (Exception rc) { System.Windows.Forms.MessageBox.Show(rc.Message); return false; } }