Esempio n. 1
0
 public ExcelTable()
 {
     TableHeader  = new ExcelTableHeader();
     TableContent = new ExcelTableContent();
 }
Esempio n. 2
0
        public static void InsertExcelTable(this Excel.Worksheet workSheet, int row, int column, ExcelTable excelTable, Action <int, int> callback, out Action stopFunc)
        {
            bool stopFlag = false;

            #region Chn 停止方法
            stopFunc = delegate()
            {
                stopFlag = true;
            };
            #endregion

            #region Chn 判断停止
            if (stopFlag == true)
            {
                return;
            }
            #endregion

            ExcelTableHeader  tableHeader       = excelTable.TableHeader;
            ExcelTableContent tableContent      = excelTable.TableContent;
            Excel.Range       tableHeaderRange  = null;
            Excel.Range       tableContentRange = null;

            List <ExcelTableColumnHeader> columnHeaderList = tableHeader.Columns.GetSortedColumnList();
            int excelColumnCount = tableHeader.Columns.ExcelColumnCount;

            //Chn 设置列样式
            workSheet.SetExcelTableColumnWidth(row, column, columnHeaderList);

            //Chn 合并表头单元格
            workSheet.MergeExcelTableCells(row, row, column, columnHeaderList);

            int headerStartRow    = row;
            int headerEndRow      = row;
            int headerStartColumn = column;
            int headerEndColumn   = column + excelColumnCount - 1;

            //Chn 设置表头(内容,字体...)
            if (tableHeader.Visible)
            {
                tableHeaderRange = workSheet.GetRange(headerStartRow, headerStartColumn, headerEndRow, headerEndColumn);

                string[,] headerStringArray = excelTable.GetHeaderStringArray();
                workSheet.SetBackgroundColor(tableHeaderRange, tableHeader.BackgroundColor);
                workSheet.SetFont(tableHeaderRange, tableHeader.Font);
                workSheet.SetBorder(tableHeaderRange, tableHeader.Border);
                if (tableHeader.RowHeight != null)
                {
                    workSheet.SetRowHeight(tableHeaderRange, (double)tableHeader.RowHeight);
                }
                workSheet.SetRangeValue(tableHeaderRange, headerStringArray);
            }

            #region Chn 判断停止
            if (stopFlag == true)
            {
                return;
            }
            #endregion

            if (tableContent.TableData == null)
            {
                return;
            }

            int contentRowCount = excelTable.TableContent.TableData.Rows.Count;

            if (tableHeader.Visible)
            {
                //Chn 设置列样式
                workSheet.SetExcelTableColumnStyle(row + 1, row + contentRowCount, column, columnHeaderList);
                //Chn 合并表体单元格
                workSheet.MergeExcelTableCells(row + 1, row + contentRowCount, column, columnHeaderList);
            }
            else
            {
                //Chn 设置列样式
                workSheet.SetExcelTableColumnStyle(row, row + contentRowCount - 1, column, columnHeaderList);
                //Chn 合并表体单元格
                workSheet.MergeExcelTableCells(row, row + contentRowCount - 1, column, columnHeaderList);
            }

            int contentStartRow;
            int contentEndRow;
            int contentStartColumn;
            int contentEndColumn;

            if (tableHeader.Visible)
            {
                contentStartRow    = row + 1;
                contentEndRow      = row + contentRowCount;
                contentStartColumn = column;
                contentEndColumn   = column + excelColumnCount - 1;
            }
            else
            {
                contentStartRow    = row;
                contentEndRow      = row + contentRowCount - 1;
                contentStartColumn = column;
                contentEndColumn   = column + excelColumnCount - 1;
            }

            tableContentRange = workSheet.GetRange(contentStartRow, contentStartColumn, contentEndRow, contentEndColumn);

            //Chn 设置表体(内容,字体...)
            string[,] contentStringArray = excelTable.GetContentStringArray();
            workSheet.SetBackgroundColor(tableContentRange, tableContent.BackgroundColor);
            workSheet.SetFont(tableContentRange, tableContent.Font);
            workSheet.SetBorder(tableContentRange, tableContent.Border);
            if (tableContent.RowHeight != null)
            {
                workSheet.SetRowHeight(tableContentRange, (double)tableContent.RowHeight);
            }
            workSheet.SetRangeValue(tableContentRange, contentStringArray);

            #region Chn 获取图片插入位置方法

            CellRectangle columnFirstCellRactangle = null;
            CellRectangle ret = new CellRectangle();
            Func <int, int, CellRectangle> getColumnCellRectangle = delegate(int tableRow, int tableColumn)
            {
                if (columnFirstCellRactangle == null)
                {
                    int cellRow = row + tableRow - 1;
                    if (tableHeader.Visible)
                    {
                        cellRow = cellRow + 1;
                    }

                    int cellStartColumn = column;
                    int cellEndColumn   = column;
                    for (int i = 0; i < tableColumn; i++)
                    {
                        ExcelTableColumnHeader columnHeader = columnHeaderList[i];

                        cellStartColumn = cellEndColumn;
                        cellEndColumn  += columnHeader.UseExcelColumnCount;
                    }
                    cellEndColumn--;

                    Excel.Range rng = workSheet.GetRange(cellRow, cellStartColumn, cellRow, cellEndColumn);

                    double top    = Convert.ToSingle(rng.Top);
                    double left   = Convert.ToSingle(rng.Left);
                    double width  = Convert.ToSingle(rng.Width);
                    double height = Convert.ToSingle(rng.Height);

                    columnFirstCellRactangle = new CellRectangle(left, top, width, height);
                }

                double retTop    = columnFirstCellRactangle.Top + (tableRow - 1) * columnFirstCellRactangle.Height;
                double retLeft   = columnFirstCellRactangle.Left;
                double retWidth  = columnFirstCellRactangle.Width;
                double retHeight = columnFirstCellRactangle.Height;

                ret.Top    = retTop;
                ret.Left   = retLeft;
                ret.Width  = retWidth;
                ret.Height = retHeight;

                return(ret);
            };

            #endregion

            #region Chn 判断停止
            if (stopFlag == true)
            {
                return;
            }
            #endregion

            #region Chn 插入图片
            for (int j = 0; j < columnHeaderList.Count; j++)
            {
                ExcelTableColumnHeader columnHeader = columnHeaderList[j];
                if (columnHeader.ColumnType == ExcelColumnType.picture)
                {
                    DataTable dt = excelTable.TableContent.TableData;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        DataRow dataRow = dt.Rows[i];
                        string  picPath = dataRow[columnHeader.ColumnName].ToString();

                        CellRectangle rect = getColumnCellRectangle(i + 1, j + 1);

                        if (File.Exists(picPath))
                        {
                            Excel.Shape pic = workSheet.InsertPictureByShape(picPath, (float)rect.Left,
                                                                             (float)rect.Top);

                            //Chn 缩放
                            if (pic.Width > rect.Width || pic.Height > rect.Height)
                            {
                                double widthK  = pic.Width / rect.Width;
                                double heightK = pic.Height / rect.Height;

                                if (widthK > heightK)
                                {
                                    pic.Width  = (float)(pic.Width / widthK);
                                    pic.Height = (float)(pic.Height / widthK);
                                }
                                else
                                {
                                    pic.Width  = (float)(pic.Width / heightK);
                                    pic.Height = (float)(pic.Height / heightK);
                                }
                            }

                            //Chn 居中
                            pic.Left += (float)((rect.Width - pic.Width) / 2);
                            pic.Top  += (float)((rect.Height - pic.Height) / 2);
                        }

                        if (callback != null)
                        {
                            callback(i + 1, dt.Rows.Count);
                        }

                        #region Chn 判断停止
                        if (stopFlag == true)
                        {
                            return;
                        }
                        #endregion
                    }

                    columnFirstCellRactangle = null;
                }
            }
            #endregion
        }