Пример #1
0
        private static string ExportMultiFile(TimGridView grid, string fileName)
        {
            string        ret                = string.Empty;
            List <string> fileList           = new List <string>();
            int           columnCount        = (grid.Columns.Count > 256) ? 256 : grid.Columns.Count;
            int           rowCount           = grid.Rows.Count;
            int           visibleColumnIndex = 0;
            bool          flag               = rowCount == 0;
            string        result;

            if (flag)
            {
                HSSFWorkbook workbook = new HSSFWorkbook();
                ExcelUtils.SetSummaryInformation(workbook);
                IDataFormat format = workbook.CreateDataFormat();
                ICellStyle  cellHeadStyleDefault = workbook.CreateCellStyle();
                IFont       fontHead             = workbook.CreateFont();
                fontHead.FontHeightInPoints = 9;
                fontHead.FontName           = "宋体";
                cellHeadStyleDefault.SetFont(fontHead);
                cellHeadStyleDefault.Alignment           = HorizontalAlignment.Center;
                cellHeadStyleDefault.FillForegroundColor = 22;
                cellHeadStyleDefault.FillPattern         = FillPattern.SolidForeground;
                ISheet sheet = workbook.CreateSheet();
                IRow   row   = sheet.CreateRow(0);
                for (int columnIndex = 0; columnIndex < columnCount; columnIndex++)
                {
                    BoundField Column = grid.Columns[columnIndex] as BoundField;
                    bool       flag2  = Column != null && Column.Visible;
                    if (flag2)
                    {
                        sheet.SetColumnWidth(visibleColumnIndex, ExcelUtils.Pixel2ExcelWidth(Column.ItemStyle.Width.Value));
                        ICell cell = row.CreateCell(visibleColumnIndex);
                        cell.CellStyle = cellHeadStyleDefault;
                        cell.SetCellValue(Column.HeaderText);
                        visibleColumnIndex++;
                    }
                }
                result = ExcelUtils.WriteExcelFile(workbook, fileName);
            }
            else
            {
                int  filesCount = rowCount / 20000;
                bool flag3      = rowCount % 20000 != 0;
                if (flag3)
                {
                    filesCount++;
                }
                for (int fileIndex = 0; fileIndex < filesCount; fileIndex++)
                {
                    HSSFWorkbook workbook2 = new HSSFWorkbook();
                    ExcelUtils.SetSummaryInformation(workbook2);
                    IDataFormat format2 = workbook2.CreateDataFormat();
                    ICellStyle  cellHeadStyleDefault2 = workbook2.CreateCellStyle();
                    ICellStyle  cellStyleDefault      = workbook2.CreateCellStyle();
                    ICellStyle  cellStyleBold         = workbook2.CreateCellStyle();
                    ICellStyle  cellStyleDate         = workbook2.CreateCellStyle();
                    ICellStyle  cellStyleDateTime     = workbook2.CreateCellStyle();
                    ICellStyle  cellStyleNumber       = workbook2.CreateCellStyle();
                    IFont       fontHead2             = workbook2.CreateFont();
                    fontHead2.FontHeightInPoints = 10;
                    fontHead2.FontName           = "宋体";
                    cellHeadStyleDefault2.SetFont(fontHead2);
                    cellHeadStyleDefault2.Alignment           = HorizontalAlignment.Center;
                    cellHeadStyleDefault2.FillForegroundColor = 22;
                    cellHeadStyleDefault2.FillPattern         = FillPattern.SolidForeground;
                    cellHeadStyleDefault2.VerticalAlignment   = VerticalAlignment.Top;
                    IFont font = workbook2.CreateFont();
                    font.FontHeightInPoints = 9;
                    font.FontName           = "宋体";
                    cellStyleDefault.SetFont(font);
                    cellStyleDefault.WrapText          = true;
                    cellStyleDefault.VerticalAlignment = VerticalAlignment.Top;
                    cellStyleDate.SetFont(font);
                    cellStyleDateTime.SetFont(font);
                    cellStyleNumber.SetFont(font);
                    cellStyleNumber.Alignment    = HorizontalAlignment.Right;
                    cellStyleDate.DataFormat     = format2.GetFormat("yyyy-MM-dd");
                    cellStyleDateTime.DataFormat = format2.GetFormat("yyyy-MM-dd HH:mm:ss");
                    cellStyleNumber.DataFormat   = format2.GetFormat("0.0000");
                    IFont fontBold = workbook2.CreateFont();
                    fontBold.FontHeightInPoints = 10;
                    fontBold.FontName           = "宋体";
                    fontBold.Boldweight         = 700;
                    cellStyleBold.SetFont(fontBold);
                    cellStyleBold.WrapText          = true;
                    cellStyleBold.VerticalAlignment = VerticalAlignment.Top;
                    ISheet sheet2 = workbook2.CreateSheet();
                    visibleColumnIndex = 0;
                    int    visibleRowIndex = 1;
                    bool   UseCheckBox     = grid.CheckBox;
                    string drValue         = string.Empty;
                    IRow   row2            = sheet2.CreateRow(0);
                    for (int columnIndex2 = 0; columnIndex2 < columnCount; columnIndex2++)
                    {
                        TimBoundField Column2 = grid.Columns[columnIndex2] as TimBoundField;
                        bool          flag4   = Column2 != null && Column2.Visible;
                        if (flag4)
                        {
                            sheet2.SetColumnWidth(visibleColumnIndex, ExcelUtils.Pixel2ExcelWidth(Column2.ItemStyle.Width.Value));
                            ICell cell2 = row2.CreateCell(visibleColumnIndex);
                            cell2.CellStyle = cellHeadStyleDefault2;
                            cell2.SetCellValue(Column2.HeaderText);
                            visibleColumnIndex++;
                        }
                    }
                    int begin = fileIndex * 20000;
                    int end   = fileIndex * 20000 + 20000;
                    end = ((end > rowCount) ? rowCount : end);
                    for (int rowIndex = begin; rowIndex < end; rowIndex++)
                    {
                        bool flag5 = grid.Rows[rowIndex].RowType != DataControlRowType.DataRow;
                        if (!flag5)
                        {
                            row2 = sheet2.CreateRow(visibleRowIndex);
                            visibleColumnIndex = 0;
                            for (int columnIndex3 = 0; columnIndex3 < columnCount; columnIndex3++)
                            {
                                TimBoundField Column2 = grid.Columns[columnIndex3] as TimBoundField;
                                bool          flag6   = Column2 != null && Column2.Visible;
                                if (flag6)
                                {
                                    ICell     cell2       = row2.CreateCell(visibleColumnIndex);
                                    TableCell gvTableCell = grid.Rows[rowIndex].Cells[UseCheckBox ? (columnIndex3 + 1) : columnIndex3];
                                    drValue = gvTableCell.Text.ToString();
                                    BoundFieldMode editType = Column2.Mode;
                                    bool           bold     = gvTableCell.Font.Bold;
                                    if (bold)
                                    {
                                        ExcelUtils.SetCellValue(cellStyleBold, cellStyleDate, cellStyleDateTime, cellStyleNumber, cell2, drValue, editType);
                                    }
                                    else
                                    {
                                        ExcelUtils.SetCellValue(cellStyleDefault, cellStyleDate, cellStyleDateTime, cellStyleNumber, cell2, drValue, editType);
                                    }
                                    visibleColumnIndex++;
                                }
                            }
                            visibleRowIndex++;
                        }
                    }
                    ret = ExcelUtils.WriteExcelFile(workbook2, fileName);
                    fileList.Add(ret);
                }
                bool flag7 = filesCount == 1;
                if (flag7)
                {
                    result = ret;
                }
                else
                {
                    string zipFileName = ExcelUtils.ZipXlsFiles(fileList);
                    result = zipFileName;
                }
            }
            return(result);
        }
Пример #2
0
 public static string Export(TimGridView grid, string fileName)
 {
     return(ExcelUtils.ExportMultiFile(grid, fileName));
 }