Ejemplo n.º 1
0
        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;
                }
            }
        }
Ejemplo n.º 2
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;
                }
            }
        }