예제 #1
0
        private static void CreateRows <T>(this HSSFWorkbook workbook, HSSFSheet sheet, Dictionary <string, string> columns, IEnumerable <T> list, string sheetName = "Sheet")
        {
            HSSFCellStyle headStyle = workbook.SetHeaderStyle();
            HSSFCellStyle cellStyle = workbook.SetCellStyle();

            // 表头
            sheet.CreateHeader(columns, headStyle);
            int rowCount   = 1;
            int sheetCount = 0;

            foreach (T item in list)
            {
                //超出65536条数据 创建新的工作簿
                if (rowCount == 65536)
                {
                    sheetCount++;
                    rowCount = 1;
                    sheet    = workbook.CreateSheet(sheetName + sheetCount);
                    sheet.CreateHeader(columns, headStyle);
                }
                //excel从第2行开始
                HSSFRow row = sheet.CreateRow(rowCount);
                workbook.CreateCells(row, columns, item, cellStyle);
                rowCount++;
            }
        }
예제 #2
0
        private static void CreateRows(this HSSFWorkbook workbook, DataRowCollection rows, Dictionary <string, string> columns, string sheetName = "Sheet")
        {
            HSSFCellStyle headStyle  = workbook.SetHeaderStyle();
            HSSFCellStyle cellStyle  = workbook.SetCellStyle();
            short         dateFormat = workbook.SetDateFormat();

            //行首
            HSSFSheet sheet = workbook.CreateSheet(sheetName);

            sheet.CreateHeader(columns, headStyle);

            int rowCount   = 1;
            int sheetCount = 1;

            foreach (DataRow dr in rows)
            {
                //超出10000条数据 创建新的工作簿
                if (rowCount == 65536)
                {
                    rowCount = 1;
                    sheetCount++;
                    sheet = workbook.CreateSheet(sheetName + sheetCount);
                    sheet.CreateHeader(columns, headStyle);
                }
                HSSFRow row = sheet.CreateRow(rowCount);
                row.CreateCells(dr, columns, cellStyle, dateFormat);
                rowCount++;
            }
        }