Example #1
0
        /// <summary>
        /// Action สำหรับเพิ่ม Row ใหม่
        /// </summary>
        /// <param name="sender"></param>
        void actionAddNewRow_Action(object sender)
        {
            if (sender is SpreadView)
            {
                SpreadView view       = (SpreadView)sender;
                int        sheetIndex = view.ActiveSheetIndex;
                if (sheetIndex < 0)
                {
                    return;
                }

                if (RowAdding != null)
                {
                    EventRowAdding eArg = new EventRowAdding();
                    RowAdding(this, eArg);
                    if (eArg.Cancel)
                    {
                        return;
                    }
                }

                SheetView sheet = view.Sheets[sheetIndex];
                if (sheet.RowCount == 0)
                { // ถ้าไม่ซักแถว
                    sheet.AddRows(0, 1);
                    sheet.SetActiveCell(sheet.RowCount - 1, 1, true);
                    sheet.SetActiveCell(sheet.RowCount - 1, 0, true);
                }
                else
                {
                    // เช็คดูก่อนว่าแถวสุดท้าย ว่างหรือไม่ ??
                    int lastRowNonEmpty = sheet.GetLastNonEmptyRow(NonEmptyItemFlag.Data);
                    if (lastRowNonEmpty == sheet.RowCount - 1)
                    {  // ถ้าแถวสุดท้ายไม่ว่าง ก็เพิ่มแถวได้เลย
                        // Add new row.
                        sheet.AddRows(sheet.RowCount, 1);
                    }

                    sheet.SetActiveCell(sheet.RowCount - 1, 0, true);
                    view.ShowActiveCell(VerticalPosition.Center, HorizontalPosition.Center);
                }

                if (RowAdded != null)
                {
                    RowAdded(this, sheet.RowCount - 1);
                }
            }
        }
Example #2
0
        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);
        }
Example #3
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;
                }
            }
        }