Esempio n. 1
0
 public static string Export(TimGridView grid, string fileName)
 {
     return(ExcelUtils.ExportMultiFile(grid, fileName));
 }
Esempio n. 2
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);
        }
Esempio n. 3
0
        private static string Export(DataTable dt, string fileName)
        {
            HSSFWorkbook workbook = new HSSFWorkbook();

            ExcelUtils.SetSummaryInformation(workbook);
            IDataFormat format            = workbook.CreateDataFormat();
            ICellStyle  cellStyleDefault  = workbook.CreateCellStyle();
            ICellStyle  cellStyleDate     = workbook.CreateCellStyle();
            ICellStyle  cellStyleDateTime = workbook.CreateCellStyle();
            ICellStyle  cellStyleNumber   = workbook.CreateCellStyle();
            IFont       font = workbook.CreateFont();

            font.FontHeightInPoints = 9;
            font.FontName           = "宋体";
            cellStyleDefault.SetFont(font);
            cellStyleDate.SetFont(font);
            cellStyleDateTime.SetFont(font);
            cellStyleNumber.SetFont(font);
            cellStyleDate.DataFormat     = format.GetFormat("yyyy-MM-dd");
            cellStyleDateTime.DataFormat = format.GetFormat("yyyy-MM-dd HH:mm:ss");
            cellStyleNumber.DataFormat   = format.GetFormat("0.0000");
            int    columnCount = dt.Columns.Count;
            int    rowCount    = dt.Rows.Count;
            ISheet sheet       = workbook.CreateSheet("Sheet1");

            for (int columnIndex = 0; columnIndex < columnCount; columnIndex++)
            {
                sheet.SetColumnWidth(columnIndex, 5120);
            }
            for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
            {
                IRow row          = sheet.CreateRow(rowIndex);
                int  columnIndex2 = 0;
                while (columnIndex2 < columnCount)
                {
                    DataColumn column  = dt.Columns[columnIndex2];
                    ICell      cell    = row.CreateCell(columnIndex2);
                    string     drValue = dt.Rows[rowIndex][columnIndex2].ToString();
                    string     text    = column.DataType.ToString();
                    uint       num     = ComputeStringHash(text);
                    if (num <= 1697786220u)
                    {
                        if (num <= 531277785u)
                        {
                            if (num != 347085918u)
                            {
                                if (num != 531277785u)
                                {
                                    goto IL_3A5;
                                }
                                if (!(text == "System.DBNull"))
                                {
                                    goto IL_3A5;
                                }
                                cell.CellStyle = cellStyleDefault;
                                cell.SetCellValue("");
                            }
                            else
                            {
                                if (!(text == "System.Boolean"))
                                {
                                    goto IL_3A5;
                                }
                                bool boolV = false;
                                bool.TryParse(drValue, out boolV);
                                cell.CellStyle = cellStyleDefault;
                                cell.SetCellValue(boolV);
                            }
                        }
                        else if (num != 848225627u)
                        {
                            if (num != 1541528931u)
                            {
                                if (num != 1697786220u)
                                {
                                    goto IL_3A5;
                                }
                                if (!(text == "System.Int16"))
                                {
                                    goto IL_3A5;
                                }
                                goto IL_33F;
                            }
                            else
                            {
                                if (!(text == "System.DateTime"))
                                {
                                    goto IL_3A5;
                                }
                                cell.CellStyle = cellStyleDateTime;
                                cell.SetCellValue(drValue);
                            }
                        }
                        else
                        {
                            if (!(text == "System.Double"))
                            {
                                goto IL_3A5;
                            }
                            goto IL_362;
                        }
                    }
                    else if (num <= 1764058053u)
                    {
                        if (num != 1741144581u)
                        {
                            if (num != 1764058053u)
                            {
                                goto IL_3A5;
                            }
                            if (!(text == "System.Int64"))
                            {
                                goto IL_3A5;
                            }
                            goto IL_33F;
                        }
                        else
                        {
                            if (!(text == "System.Decimal"))
                            {
                                goto IL_3A5;
                            }
                            goto IL_362;
                        }
                    }
                    else if (num != 3079944380u)
                    {
                        if (num != 4180476474u)
                        {
                            if (num != 4201364391u)
                            {
                                goto IL_3A5;
                            }
                            if (!(text == "System.String"))
                            {
                                goto IL_3A5;
                            }
                            cell.CellStyle = cellStyleDefault;
                            cell.SetCellValue(drValue);
                        }
                        else
                        {
                            if (!(text == "System.Int32"))
                            {
                                goto IL_3A5;
                            }
                            goto IL_33F;
                        }
                    }
                    else
                    {
                        if (!(text == "System.Byte"))
                        {
                            goto IL_3A5;
                        }
                        goto IL_33F;
                    }
IL_3BD:
                    columnIndex2++;
                    continue;
IL_33F:
                    int intV = 0;
                    int.TryParse(drValue, out intV);
                    cell.CellStyle = cellStyleDefault;
                    cell.SetCellValue((double)intV);
                    goto IL_3BD;
IL_362:
                    double doubV = 0.0;
                    double.TryParse(drValue, out doubV);
                    cell.CellStyle = cellStyleNumber;
                    cell.SetCellValue(doubV);
                    goto IL_3BD;
IL_3A5:
                    cell.CellStyle = cellStyleDefault;
                    cell.SetCellValue("");
                    goto IL_3BD;
                }
            }
            return(ExcelUtils.WriteExcelFile(workbook, fileName));
        }