public static bool Excel(FarPoint.Win.Spread.FpSpread fpSpread, string fileName) { try { SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "excel files (*.xls)|*.xls|All files (*.*)|*.*"; saveFileDialog1.FilterIndex = 0; saveFileDialog1.Title = "Excel 파일저장"; saveFileDialog1.AddExtension = true; saveFileDialog1.RestoreDirectory = true; saveFileDialog1.FileName = fileName + ".xls"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { //SaveFlags로 ColumnHeader를 Excel파일에 함께 저장함 fpSpread.SaveExcel(saveFileDialog1.FileName, FarPoint.Excel.ExcelSaveFlags.SaveCustomColumnHeaders); return(true); } else { return(false); } } catch (Exception ex) { MessageBox.Show("Error : [" + ex.Message + "]", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return(false); } }
public static bool Excel_SaveAs(FarPoint.Win.Spread.FpSpread fp) { SaveFileDialog sf = new SaveFileDialog(); sf.Filter = "Excel files (*.xls)|*.xls |All files (*.*) | *.*"; if (sf.ShowDialog() != DialogResult.OK) { return(false); } //if (sf.FileName == null || sf.FileName == string.Empty) return; fp.SaveExcel(sf.FileName, FarPoint.Win.Spread.Model.IncludeHeaders.ColumnHeadersCustomOnly); return(true); }
private static void CreateTitle(string fname, string title, string dw) { FarPoint.Win.Spread.FpSpread fps = new FarPoint.Win.Spread.FpSpread(); fps.OpenExcel(fname); SheetView sv = fps.Sheets[0]; int ColumnCount = sv.NonEmptyColumnCount; int RowCount = sv.NonEmptyRowCount; //sv.ColumnCount = ColumnCount; //sv.RowCount = RowCount; sv.AddRows(0, 2); sv.Cells[0, 0].Text = title; sv.Cells[0, 0].Font = new System.Drawing.Font("宋体", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); sv.Cells[0, 0].HorizontalAlignment = CellHorizontalAlignment.Center; sv.Cells[0, 0].VerticalAlignment = CellVerticalAlignment.Center; sv.Cells[0, 0].Row.Height = 50; sv.Cells[0, 0].ColumnSpan = ColumnCount; sv.Cells[1, 0].Text = dw; sv.Cells[1, 0].Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); sv.Cells[1, 0].HorizontalAlignment = CellHorizontalAlignment.Right; sv.Cells[1, 0].VerticalAlignment = CellVerticalAlignment.Center; sv.Cells[1, 0].ColumnSpan = ColumnCount; for (int i = 0; i < ColumnCount; i++) { sv.Cells[2, i].Row.Height = 40; sv.Cells[2, i].Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); } sv.AddRows(RowCount + 2, 2); sv.Cells[RowCount + 2, 0].Text = "建表时间:" + DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day; sv.Cells[RowCount + 2, 0].Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); sv.Cells[RowCount + 2, 0].HorizontalAlignment = CellHorizontalAlignment.Right; sv.Cells[RowCount + 2, 0].VerticalAlignment = CellVerticalAlignment.Center; sv.Cells[RowCount + 2, 0].ColumnSpan = ColumnCount; fps.SaveExcel(fname); }
public static void ExportExcel(GridControl gridControl) { //try //{ SaveFileDialog saveFileDialog1 = new SaveFileDialog(); string fname = ""; saveFileDialog1.Filter = "Microsoft Excel (*.xls)|*.xls"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { fname = saveFileDialog1.FileName; try { gridControl.ExportToExcelOld(fname); FarPoint.Win.Spread.FpSpread fps = new FarPoint.Win.Spread.FpSpread(); fps.OpenExcel(fname); SheetView sv = fps.Sheets[0]; for (int j = 0; j < sv.NonEmptyRowCount; j++) { for (int k = 0; k < sv.NonEmptyColumnCount; k++) { sv.Cells[j,k].CellType = new FarPoint.Win.Spread.CellType.NumberCellType(); } } fps.SaveExcel(fname); // 定义要使用的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(); workBook = excelApp.Workbooks.Open(fname, 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.Select(); range.NumberFormatLocal = "G/通用格式"; //保护工作表 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(); if (MsgBox.ShowYesNo("导出成功,是否打开该文档?") != DialogResult.Yes) return; System.Diagnostics.Process.Start(fname); } catch { MsgBox.Show("无法保存" + fname + "。请用其他文件名保存文件,或将文件存至其他位置。"); return; } } //return true; //} //catch { } }
public static void ExportToExcelOld(string title, string dw, GridControl gc) { SaveFileDialog saveFileDialog1 = new SaveFileDialog(); string fname = ""; saveFileDialog1.Filter = "Microsoft Excel (*.xls)|*.xls"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { try { fname = saveFileDialog1.FileName; gc.ExportToExcelOld(fname); // gc.ExportToExcelOld(fname); FarPoint.Win.Spread.FpSpread fps = new FarPoint.Win.Spread.FpSpread(); fps.OpenExcel(fname); SheetView sv = fps.Sheets[0]; int ColumnCount = sv.NonEmptyColumnCount+2; int RowCount = sv.NonEmptyRowCount; //sv.ColumnCount = ColumnCount; //sv.RowCount = RowCount; sv.AddRows(0, 2); sv.Cells[0, 0].Text = title; sv.Cells[0, 0].Font = new System.Drawing.Font("����", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); sv.Cells[0, 0].HorizontalAlignment = CellHorizontalAlignment.Center; sv.Cells[0, 0].VerticalAlignment = CellVerticalAlignment.Center; sv.Cells[0, 0].Row.Height = 50; sv.Cells[0, 0].ColumnSpan = ColumnCount; sv.Cells[1, 0].Text = dw; sv.Cells[1, 0].Font = new System.Drawing.Font("����", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); sv.Cells[1, 0].HorizontalAlignment = CellHorizontalAlignment.Right; sv.Cells[1, 0].VerticalAlignment = CellVerticalAlignment.Center; sv.Cells[1, 0].ColumnSpan = ColumnCount; for (int i = 0; i < ColumnCount; i++) { sv.Cells[2, i].Column.Width = 150; sv.Cells[2, i].Row.Height = 40; sv.Cells[2, i].Font = new System.Drawing.Font("����", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); } //FarPoint.Win.Spread.CellType.NumberCellType numberCellTypes= new FarPoint.Win.Spread.CellType.NumberCellType(); //numberCellTypes.DecimalPlaces = 1; //for (int i = 0; i < ColumnCount; i++) //{ // for (int j = 2; i < RowCount; i++) // { // sv.Cells[j, i].CellType = numberCellTypes; // } //} sv.AddRows(RowCount + 2, 2); sv.Cells[RowCount + 2, 0].Text = "����ʱ��:" + DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day; sv.Cells[RowCount + 2, 0].Font = new System.Drawing.Font("����", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); sv.Cells[RowCount + 2, 0].HorizontalAlignment = CellHorizontalAlignment.Right; sv.Cells[RowCount + 2, 0].VerticalAlignment = CellVerticalAlignment.Center; sv.Cells[RowCount + 2, 0].ColumnSpan = ColumnCount; //sv.SetColumnVisible(ColumnCount - 1, false); //sv.SetColumnVisible(ColumnCount - 2, false); for (int j = 0; j < sv.NonEmptyRowCount; j++) { for (int k = 0; k < sv.NonEmptyColumnCount; k++) { sv.Cells[j,k].CellType = new FarPoint.Win.Spread.CellType.NumberCellType(); } } fps.SaveExcel(fname); // ����Ҫʹ�õ�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(); workBook = excelApp.Workbooks.Open(fname, 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.Select(); range.NumberFormatLocal = "G/ͨ�ø�ʽ"; //���������� 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(); if (MsgBox.ShowYesNo("�����ɹ����Ƿ���ĵ���") != DialogResult.Yes) return; System.Diagnostics.Process.Start(fname); } catch { MsgBox.Show("������" + fname + "�����������ļ��������ļ������ļ���������λ�á�"); return; } } }
protected virtual void Export() { if (this.bTabControl1.SelectedIndex == 0) { //this.bsprData.Export(false); string file = ""; bool bProtect = this.bsprData.ActiveSheet.Protect; this.bsprData.ActiveSheet.Protect = false; SaveFileDialog openDlg = new SaveFileDialog(); openDlg.Filter = "Excel Files (*.xls)|*.xls"; openDlg.FileName = ""; openDlg.DefaultExt = ".xls"; openDlg.CheckFileExists = false; openDlg.CheckPathExists = true; DialogResult res = openDlg.ShowDialog(); if (res != DialogResult.OK) { return; } file = openDlg.FileName; FarPoint.Win.Spread.SheetView spread_Sheet1 = new FarPoint.Win.Spread.SheetView(); spread_Sheet1.SheetName = "_ExcelExportSheet"; FarPoint.Win.Spread.FpSpread spread = new FarPoint.Win.Spread.FpSpread(); spread.Sheets.Add(spread_Sheet1); spread_Sheet1.Visible = true; spread.ActiveSheet = spread_Sheet1; byte[] buffer = null; System.IO.MemoryStream stream = null; this.bsprData.SetFilterVisible(false); try { stream = new System.IO.MemoryStream(); this.bsprData.Save(stream, false); buffer = stream.ToArray(); stream.Close(); } catch (Exception ex) { throw ex; } finally { if (stream != null) { stream.Dispose(); stream = null; } } stream = new System.IO.MemoryStream(buffer); spread.Open(stream); if (stream != null) { stream.Dispose(); stream = null; } for (int i = spread.ActiveSheet.Columns.Count - 1; i >= 0; i--) { if (!spread.ActiveSheet.Columns[i].Visible) { spread.ActiveSheet.Columns[i].Remove(); } } spread.SaveExcel(file, FarPoint.Win.Spread.Model.IncludeHeaders.ColumnHeadersCustomOnly); this.bsprData.ActiveSheet.Protect = bProtect; string strMessage = "It was saved successfully. Do you open saved file?"; DialogResult result = MessageBox.Show(strMessage, "Open", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Classes\Applications\EXCEL.EXE"); if (key == null) { MSGHandler.DisplayMessage(MSGType.Error, "SPC_INFO_NEED_MS_OFFICE", null, null); } else { System.Diagnostics.Process process = new System.Diagnostics.Process(); process.StartInfo.FileName = file; process.Start(); } } } else { string file = ""; bool bProtect = this.bsprRawData.ActiveSheet.Protect; this.bsprRawData.ActiveSheet.Protect = false; SaveFileDialog openDlg = new SaveFileDialog(); openDlg.Filter = "Excel Files (*.xls)|*.xls"; openDlg.FileName = ""; openDlg.DefaultExt = ".xls"; openDlg.CheckFileExists = false; openDlg.CheckPathExists = true; DialogResult res = openDlg.ShowDialog(); if (res != DialogResult.OK) { return; } file = openDlg.FileName; FarPoint.Win.Spread.SheetView spread_Sheet1 = new FarPoint.Win.Spread.SheetView(); spread_Sheet1.SheetName = "_ExcelExportSheet"; FarPoint.Win.Spread.FpSpread spread = new FarPoint.Win.Spread.FpSpread(); spread.Sheets.Add(spread_Sheet1); spread_Sheet1.Visible = true; spread.ActiveSheet = spread_Sheet1; byte[] buffer = null; System.IO.MemoryStream stream = null; this.bsprRawData.SetFilterVisible(false); try { stream = new System.IO.MemoryStream(); this.bsprRawData.Save(stream, false); buffer = stream.ToArray(); stream.Close(); } catch (Exception ex) { throw ex; } finally { if (stream != null) { stream.Dispose(); stream = null; } } stream = new System.IO.MemoryStream(buffer); spread.Open(stream); if (stream != null) { stream.Dispose(); stream = null; } for (int i = spread.ActiveSheet.Columns.Count - 1; i >= 0; i--) { if (!spread.ActiveSheet.Columns[i].Visible) { spread.ActiveSheet.Columns[i].Remove(); } } spread.SaveExcel(file, FarPoint.Win.Spread.Model.IncludeHeaders.ColumnHeadersCustomOnly); this.bsprRawData.ActiveSheet.Protect = bProtect; string strMessage = "It was saved successfully. Do you open saved file?"; DialogResult result = MessageBox.Show(strMessage, "Open", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Classes\Applications\EXCEL.EXE"); if (key == null) { MSGHandler.DisplayMessage(MSGType.Error, "SPC_INFO_NEED_MS_OFFICE", null, null); } else { System.Diagnostics.Process process = new System.Diagnostics.Process(); process.StartInfo.FileName = file; process.Start(); } } } }
public static void ExportToExcelOld(string title, string dw, GridControl gc) { SaveFileDialog saveFileDialog1 = new SaveFileDialog(); string fname = ""; saveFileDialog1.Filter = "Microsoft Excel (*.xls)|*.xls"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { try { fname = saveFileDialog1.FileName; gc.ExportToExcelOld(fname); FarPoint.Win.Spread.FpSpread fps = new FarPoint.Win.Spread.FpSpread(); fps.OpenExcel(fname); SheetView sv = fps.Sheets[0]; int ColumnCount = sv.NonEmptyColumnCount; int RowCount = sv.NonEmptyRowCount; //sv.ColumnCount = ColumnCount; //sv.RowCount = RowCount; sv.AddRows(0, 2); sv.Cells[0, 0].Text = title; sv.Cells[0, 0].Font = new System.Drawing.Font("宋体", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); sv.Cells[0, 0].HorizontalAlignment = CellHorizontalAlignment.Center; sv.Cells[0, 0].VerticalAlignment = CellVerticalAlignment.Center; sv.Cells[0, 0].Row.Height = 50; sv.Cells[0, 0].ColumnSpan = ColumnCount; sv.Cells[1, 0].Text = dw; sv.Cells[1, 0].Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); sv.Cells[1, 0].HorizontalAlignment = CellHorizontalAlignment.Right; sv.Cells[1, 0].VerticalAlignment = CellVerticalAlignment.Center; sv.Cells[1, 0].ColumnSpan = ColumnCount; for (int i = 0; i < ColumnCount; i++) { sv.Cells[2, i].Row.Height = 40; sv.Cells[2, i].Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); } sv.AddRows(RowCount + 2, 2); sv.Cells[RowCount + 2, 0].Text = "建表时间:" + DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day; sv.Cells[RowCount + 2, 0].Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); sv.Cells[RowCount + 2, 0].HorizontalAlignment = CellHorizontalAlignment.Right; sv.Cells[RowCount + 2, 0].VerticalAlignment = CellVerticalAlignment.Center; sv.Cells[RowCount + 2, 0].ColumnSpan = ColumnCount; sv.SetColumnVisible(ColumnCount - 1, false); sv.SetColumnVisible(ColumnCount - 2, false); for (int j = 0; j < sv.NonEmptyRowCount; j++) { for (int k = 0; k < sv.NonEmptyColumnCount; k++) { sv.Cells[j, k].CellType = new FarPoint.Win.Spread.CellType.NumberCellType(); } } fps.SaveExcel(fname); // 定义要使用的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(); workBook = excelApp.Workbooks.Open(fname, 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.Select(); range.NumberFormatLocal = "G/通用格式"; //保护工作表 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(); if (MsgBox.ShowYesNo("导出成功,是否打开该文档?") != DialogResult.Yes) { return; } System.Diagnostics.Process.Start(fname); } catch { MsgBox.Show("无法保存" + fname + "。请用其他文件名保存文件,或将文件存至其他位置。"); return; } } }
public static void ExportExcel(GridControl gridControl) { //try //{ SaveFileDialog saveFileDialog1 = new SaveFileDialog(); string fname = ""; saveFileDialog1.Filter = "Microsoft Excel (*.xls)|*.xls"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { fname = saveFileDialog1.FileName; //File.SetAttributes(fname, File.GetAttributes(fname) | FileAttributes.ReadOnly); ////Create the file. //using (FileStream fs = new FileStream(fname, FileMode.OpenOrCreate, FileAccess.Read)) //{ // if (!fs.CanWrite) // { // MsgBox.Show("文件不能被创建,请检查文件是否被打开"); // return; // } //} // try //{ // File.Move(fname,fname+"1"); //} //catch //{ // MsgBox.Show("无法保存"+fname+"。请用其他文件名保存文件,或将文件存至其他位置。"); // return; //} //finally //{ // File.Move(fname + "1",fname); //} try { gridControl.ExportToExcelOld(fname); FarPoint.Win.Spread.FpSpread fps = new FarPoint.Win.Spread.FpSpread(); fps.OpenExcel(fname); SheetView sv = fps.Sheets[0]; for (int j = 0; j < sv.NonEmptyRowCount; j++) { for (int k = 0; k < sv.NonEmptyColumnCount; k++) { FarPoint.Win.Spread.CellType.NumberCellType temptype = new FarPoint.Win.Spread.CellType.NumberCellType(); sv.Cells[j, k].CellType = temptype; } } fps.SaveExcel(fname); // 定义要使用的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(); workBook = excelApp.Workbooks.Open(fname, 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.Select(); range.NumberFormatLocal = "G/通用格式"; //保护工作表 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(); if (MsgBox.ShowYesNo("导出成功,是否打开该文档?") != DialogResult.Yes) { return; } System.Diagnostics.Process.Start(fname); } catch { MsgBox.Show("无法保存" + fname + "。请用其他文件名保存文件,或将文件存至其他位置。"); return; } } //return true; //} //catch { } }
private static void CreateTitle(string fname,string title,string dw) { FarPoint.Win.Spread.FpSpread fps = new FarPoint.Win.Spread.FpSpread(); fps.OpenExcel(fname); SheetView sv = fps.Sheets[0]; int ColumnCount = sv.NonEmptyColumnCount; int RowCount = sv.NonEmptyRowCount; //sv.ColumnCount = ColumnCount; //sv.RowCount = RowCount; sv.AddRows(0, 2); sv.Cells[0, 0].Text = title; sv.Cells[0, 0].Font = new System.Drawing.Font("����", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); sv.Cells[0, 0].HorizontalAlignment = CellHorizontalAlignment.Center; sv.Cells[0, 0].VerticalAlignment = CellVerticalAlignment.Center; sv.Cells[0, 0].Row.Height = 50; sv.Cells[0, 0].ColumnSpan = ColumnCount; sv.Cells[1, 0].Text = dw; sv.Cells[1, 0].Font = new System.Drawing.Font("����", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); sv.Cells[1, 0].HorizontalAlignment = CellHorizontalAlignment.Right; sv.Cells[1, 0].VerticalAlignment = CellVerticalAlignment.Center; sv.Cells[1, 0].ColumnSpan = ColumnCount; for (int i = 0; i < ColumnCount; i++) { sv.Cells[2, i].Row.Height = 40; sv.Cells[2, i].Font = new System.Drawing.Font("����", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); } sv.AddRows(RowCount + 2, 2); sv.Cells[RowCount + 2, 0].Text = "����ʱ��:" + DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day; sv.Cells[RowCount + 2, 0].Font = new System.Drawing.Font("����", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); sv.Cells[RowCount + 2, 0].HorizontalAlignment = CellHorizontalAlignment.Right; sv.Cells[RowCount + 2, 0].VerticalAlignment = CellVerticalAlignment.Center; sv.Cells[RowCount + 2, 0].ColumnSpan = ColumnCount; fps.SaveExcel(fname); }
public static void ExportToExcelOld(GridControl gridControl,string title,string dw) { //try //{ // Control.CheckForIllegalCrossThreadCalls = false; SaveFileDialog saveFileDialog1 = new SaveFileDialog(); string fname = ""; saveFileDialog1.Filter = "Microsoft Excel (*.xls)|*.xls"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { FrmGress frm = new FrmGress(); // Thread thread = new Thread(new ThreadStart(frm.Show)); // thread.Start(); fname = saveFileDialog1.FileName; try { gridControl.ExportToExcelOld(fname); CreateTitle(fname, title, dw); FarPoint.Win.Spread.FpSpread fps = new FarPoint.Win.Spread.FpSpread(); fps.OpenExcel(fname); SheetView sv = fps.Sheets[0]; for (int j = 0; j < sv.NonEmptyRowCount; j++) { for (int k = 0; k < sv.NonEmptyColumnCount; k++) { sv.Cells[j, k].CellType = new FarPoint.Win.Spread.CellType.NumberCellType(); } } fps.SaveExcel(fname); // ����Ҫʹ�õ�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(); workBook = excelApp.Workbooks.Open(fname, 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.Select(); range.NumberFormatLocal = "G/ͨ�ø�ʽ"; //���������� 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(); if (MsgBox.ShowYesNo("�����ɹ����Ƿ���ĵ���") != DialogResult.Yes) return; System.Diagnostics.Process.Start(fname); } catch { MsgBox.Show("������" + fname + "�����������ļ��������ļ������ļ���������λ�á�"); return; } } }
/// <summary> /// 导出EXCEL /// </summary> /// <param name="gridControl"></param> /// <param name="title"></param> /// <param name="dw"></param> public static void ExportToExcelandchildren(GridControl gridControl, string title, string dw) { SaveFileDialog saveFileDialog1 = new SaveFileDialog(); string fname = ""; saveFileDialog1.Filter = "Microsoft Excel (*.xls)|*.xls"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { fname = saveFileDialog1.FileName; try { gridControl.ExportToExcelOld(fname); FarPoint.Win.Spread.FpSpread fps = new FarPoint.Win.Spread.FpSpread(); fps.OpenExcel(fname); SheetView sv = fps.Sheets[0]; //for (int j = 1; j < sv.NonEmptyColumnCount; j++) //{ // for (int k = 0; k < sv.NonEmptyRowCount; k++) // { // sv.Cells[k, j].CellType = new FarPoint.Win.Spread.CellType.NumberCellType(); // } //} //输出子表的数据 //输出母线 ExportChildEXCEL(gridControl.DataSource as DataTable, new UCDeviceMX(), fps, "母线", "01"); //输出两绕组 ExportChildEXCEL(gridControl.DataSource as DataTable, new UCDeviceBYQ2(), fps, "两绕组变压器", "02"); //输出三绕组变压器 ExportChildEXCEL(gridControl.DataSource as DataTable, new UCDeviceBYQ3(), fps, "三绕组变压器", "03"); //输出负荷 ExportChildEXCEL(gridControl.DataSource as DataTable, new UCDeviceFH(), fps, "负荷", "12"); //输出发电机 ExportChildEXCEL(gridControl.DataSource as DataTable, new UCDeviceFDJ(), fps, "发电机", "04"); fps.SaveExcel(fname); //// 定义要使用的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(); //workBook = excelApp.Workbooks.Open(fname, 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.Select(); // range.NumberFormatLocal = "G/通用格式"; // //保护工作表 // 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(); // CreateTitle(fname, title, dw); if (MsgBox.ShowYesNo("导出成功,是否打开该文档?") != DialogResult.Yes) { return; } System.Diagnostics.Process.Start(fname); } catch (Exception ee) { MsgBox.Show("无法保存" + fname + "。请用其他文件名保存文件,或将文件存至其他位置。"); return; } } }
/// <summary> /// 导出EXCEL /// </summary> /// <param name="gridControl"></param> /// <param name="title"></param> /// <param name="dw"></param> public static void ExportToExcelOld(GridControl gridControl, string title, string dw) { SaveFileDialog saveFileDialog1 = new SaveFileDialog(); string fname = ""; saveFileDialog1.Filter = "Microsoft Excel (*.xls)|*.xls"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { fname = saveFileDialog1.FileName; try { gridControl.ExportToExcelOld(fname); FarPoint.Win.Spread.FpSpread fps = new FarPoint.Win.Spread.FpSpread(); fps.OpenExcel(fname); SheetView sv = fps.Sheets[0]; //for (int j = 1; j < sv.NonEmptyColumnCount; j++) //{ // for (int k = 0; k < sv.NonEmptyRowCount; k++) // { // sv.Cells[k, j].CellType = new FarPoint.Win.Spread.CellType.NumberCellType(); // } //} fps.SaveExcel(fname); // 定义要使用的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(); workBook = excelApp.Workbooks.Open(fname, 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.Select(); range.NumberFormatLocal = "G/通用格式"; //保护工作表 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(); // CreateTitle(fname, title, dw); if (MsgBox.ShowYesNo("导出成功,是否打开该文档?") != DialogResult.Yes) { return; } System.Diagnostics.Process.Start(fname); } catch (Exception ee) { MsgBox.Show("无法保存" + fname + "。请用其他文件名保存文件,或将文件存至其他位置。"); return; } } }
public static void ExportExcel(GridControl gridControl) { //try //{ SaveFileDialog saveFileDialog1 = new SaveFileDialog(); string fname = ""; saveFileDialog1.Filter = "Microsoft Excel (*.xls)|*.xls"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { fname = saveFileDialog1.FileName; //File.SetAttributes(fname, File.GetAttributes(fname) | FileAttributes.ReadOnly); ////Create the file. //using (FileStream fs = new FileStream(fname, FileMode.OpenOrCreate, FileAccess.Read)) //{ // if (!fs.CanWrite) // { // MsgBox.Show("�ļ����ܱ������������ļ��Ƿ�"); // return; // } //} // try //{ // File.Move(fname,fname+"1"); //} //catch //{ // MsgBox.Show("������"+fname+"�����������ļ��������ļ������ļ���������λ�á�"); // return; //} //finally //{ // File.Move(fname + "1",fname); //} try { gridControl.ExportToExcelOld(fname); FarPoint.Win.Spread.FpSpread fps = new FarPoint.Win.Spread.FpSpread(); fps.OpenExcel(fname); SheetView sv = fps.Sheets[0]; for (int j = 0; j < sv.NonEmptyRowCount; j++) { for (int k = 0; k < sv.NonEmptyColumnCount; k++) { FarPoint.Win.Spread.CellType.NumberCellType temptype=new FarPoint.Win.Spread.CellType.NumberCellType(); sv.Cells[j, k].CellType = temptype; } } fps.SaveExcel(fname); // ����Ҫʹ�õ�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(); workBook = excelApp.Workbooks.Open(fname, 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.Select(); range.NumberFormatLocal="G/ͨ�ø�ʽ"; //���������� 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(); if (MsgBox.ShowYesNo("�����ɹ����Ƿ���ĵ���") != DialogResult.Yes) return; System.Diagnostics.Process.Start(fname); } catch { MsgBox.Show("������"+fname+"�����������ļ��������ļ������ļ���������λ�á�"); return; } } //return true; //} //catch { } }
public static void ExportToExcelOld(GridControl gridControl, int row, string col, bool bHe) { //try //{ // Control.CheckForIllegalCrossThreadCalls = false; SaveFileDialog saveFileDialog1 = new SaveFileDialog(); string fname = ""; saveFileDialog1.Filter = "Microsoft Excel (*.xls)|*.xls"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { FrmGress frm = new FrmGress(); // Thread thread = new Thread(new ThreadStart(frm.Show)); // thread.Start(); fname = saveFileDialog1.FileName; try { gridControl.ExportToExcelOld(fname); if (bHe) { FarPoint.Win.Spread.FpSpread fps = new FarPoint.Win.Spread.FpSpread(); fps.OpenExcel(fname); SheetView sv = fps.Sheets[0]; for (int j = 0; j < sv.NonEmptyRowCount; j++) { for (int k = 0; k < sv.NonEmptyColumnCount; k++) { sv.Cells[j, k].CellType = new FarPoint.Win.Spread.CellType.NumberCellType(); } } fps.SaveExcel(fname); // 定义要使用的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(); workBook = excelApp.Workbooks.Open(fname, 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 row1 = ws.UsedRange.Rows.Count; //有数据的列数 int col1 = ws.UsedRange.Columns.Count; //创建一个区域 range = ws.get_Range(ws.Cells[1, 1], ws.Cells[row1, col1]); //设区域内的单元格自动换行 range.Select(); range.NumberFormatLocal = "G/通用格式"; //保护工作表 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(); ChangeExcel(fname, row, col); } else { FarPoint.Win.Spread.FpSpread fps = new FarPoint.Win.Spread.FpSpread(); fps.OpenExcel(fname); SheetView sv = fps.Sheets[0]; for (int j = 0; j < sv.NonEmptyRowCount; j++) { for (int k = 0; k < sv.NonEmptyColumnCount; k++) { sv.Cells[j, k].CellType = new FarPoint.Win.Spread.CellType.NumberCellType(); } } fps.SaveExcel(fname); // 定义要使用的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(); workBook = excelApp.Workbooks.Open(fname, 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 row1 = ws.UsedRange.Rows.Count; //有数据的列数 int col1 = ws.UsedRange.Columns.Count; //创建一个区域 range = ws.get_Range(ws.Cells[1, 1], ws.Cells[row1, col1]); //设区域内的单元格自动换行 range.Select(); range.NumberFormatLocal = "G/通用格式"; //保护工作表 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(); if (MsgBox.ShowYesNo("导出成功,是否打开该文档?") != DialogResult.Yes) { return; } System.Diagnostics.Process.Start(fname); } } catch { MsgBox.Show("无法保存" + fname + "。请用其他文件名保存文件,或将文件存至其他位置。"); return; } } }