Example #1
0
 private void AddData(FarPoint.Win.Spread.FpSpread obj)
 {
     try
     {
         wait1 = new WaitDialogForm("", "正在加载数据, 请稍候...");
         //打开Excel表格
         //清空工作表
         fpSpread1.Sheets.Clear();
         obj.OpenExcel(System.Windows.Forms.Application.StartupPath + "\\xls\\Sheet10_1.xls");
         PF.SpreadRemoveEmptyCells(obj);
         this.barEditItem1.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
         this.AddCellChanged();
         S10_11.AddBarEditItems(this.barEditItem1, this.barEditItem2, this);//添加下拉菜单内容
         wait1.Close();
     }
     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\\Sheet10_1.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\\Sheet10_1.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();
     }
     wait1.Close();
 }
Example #2
0
        //导出
        private void barBtnDaochuExcel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            string         fname           = "";

            saveFileDialog1.Filter = "Microsoft Excel (*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                fname = saveFileDialog1.FileName;

                try
                {
                    fpSpread1.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.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();
                    if (MessageBox.Show("导出成功,是否打开该文档?", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                    {
                        return;
                    }

                    System.Diagnostics.Process.Start(fname);
                }
                catch
                {
                    MessageBox.Show("无法保存" + fname + "。请用其他文件名保存文件,或将文件存至其他位置。");
                    return;
                }
            }
        }
Example #3
0
        //保存
        private void barBtnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            WaitDialogForm wait = null;

            wait = new WaitDialogForm("", "正在保存数据, 请稍候...");
            //判断文件夹是否存在,不存在则创建
            if (!Directory.Exists(Tcommon.CurrentPath + Tcommon.ExcelDir))
            {
                Directory.CreateDirectory(Tcommon.CurrentPath + Tcommon.ExcelDir);
            }
            //保存excel文件
            try
            {
                //保存excel文件
                fpSpread1.SaveExcel(Tcommon.ExcelFilePath + ExcelName);
                //以下是打开文件设表格自动换行

                // 定义要使用的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 = Tcommon.ExcelFilePath + ExcelName;
                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();
                MessageBox.Show("保存成功");
            }
            catch (System.Exception ee)
            {
                wait.Close();
                MessageBox.Show("保存错误!确定您安装有Office Excel,或者关闭所有Excel文件重试");
            }
        }
Example #4
0
        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;
                }
            }
        }
Example #5
0
        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 { }
        }
Example #6
0
        /// <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;
                }
            }
        }
Example #7
0
        private void fpSpread_addsheet()
        {
            WaitDialogForm wait = null;

            wait = new WaitDialogForm("", "正在加载数据, 请稍候...");
            try
            {
                //打开Excel表格
                //清空工作表
                fpSpread1.Sheets.Clear();
                fpSpread1.OpenExcel(Tcommon.ExcelFilePath + ExcelName);
                TC.SpreadRemoveEmptyCells(fpSpread1);
            }
            catch (System.Exception e)
            {
                //如果打开出错则重新生成并保存
                fpSpread1.Sheets.Clear();
                Firstadddata();
                //判断文件夹是否存在,不存在则创建
                if (!Directory.Exists(Tcommon.CurrentPath + Tcommon.ExcelDir))
                {
                    Directory.CreateDirectory(Tcommon.CurrentPath + Tcommon.ExcelDir);
                }
                //保存excel文件
                fpSpread1.SaveExcel(Tcommon.ExcelFilePath + ExcelName);
                //以下是打开文件设表格自动换行

                // 定义要使用的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 = Tcommon.ExcelFilePath + ExcelName;
                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();
        }
Example #8
0
        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;
                }
            }
        }