Search in sources :

Example 51 with HSSFRow

use of org.apache.poi.hssf.usermodel.HSSFRow in project nextprot-api by calipho-sib.

the class EntryOverviewXLSWriterTest method assertXLSEquals.

private static void assertXLSEquals(ByteArrayOutputStream baos, String[] headers, Object[] values) throws IOException {
    InputStream is = new ByteArrayInputStream(baos.toByteArray());
    HSSFWorkbook workbook = new HSSFWorkbook(is);
    HSSFSheet worksheet = workbook.getSheet("Proteins");
    HSSFRow headerRow = worksheet.getRow(0);
    HSSFRow valuesRow = worksheet.getRow(1);
    for (int i = 0; i < headers.length; i++) {
        Assert.assertEquals(headers[i], headerRow.getCell(i).getStringCellValue());
        if (i >= 7 && i <= 9)
            Assert.assertEquals(values[i], valuesRow.getCell(i).getNumericCellValue());
        else
            Assert.assertEquals(values[i], valuesRow.getCell(i).getStringCellValue());
    }
    workbook.close();
}
Also used : HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 52 with HSSFRow

use of org.apache.poi.hssf.usermodel.HSSFRow in project Hotchpotch by carryxyh.

the class ExportExcelUtils method createTitleRow.

/**
 * 生成标题(第零行创建)
 *
 * @param titleMap  对象属性名称->表头显示名称
 * @param sheetName sheet名称
 */
private static void createTitleRow(Map<String, String> titleMap, String sheetName) {
    CellRangeAddress titleRange = new CellRangeAddress(0, 0, 0, titleMap.size() - 1);
    sheet.addMergedRegion(titleRange);
    HSSFRow titleRow = sheet.createRow(TITLE_START_POSITION);
    HSSFCell titleCell = titleRow.createCell(0);
    titleCell.setCellValue(sheetName);
}
Also used : HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress)

Example 53 with HSSFRow

use of org.apache.poi.hssf.usermodel.HSSFRow in project zoj by licheng.

the class ShowRankListAction method exportToExcel.

private byte[] exportToExcel(AbstractContest contest, List<Problem> problems, RankList ranklist) throws Exception {
    List<RankListEntry> entries = ranklist.getEntries();
    long time = this.getTimeEscaped(contest);
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet();
    HSSFRow row = sheet.createRow(0);
    HSSFCell cell = row.createCell((short) 0);
    cell.setCellValue(contest.getTitle());
    if (ranklist.getRole() != null) {
        row = sheet.createRow(1);
        cell = row.createCell((short) 0);
        cell.setEncoding(HSSFCell.ENCODING_UTF_16);
        cell.setCellValue(ranklist.getRole().getDescription());
    }
    row = sheet.createRow(2);
    cell = row.createCell((short) 0);
    cell.setCellValue("Length");
    cell = row.createCell((short) 1);
    cell.setCellValue(Utility.toTime(contest.getLength() / 1000));
    row = sheet.createRow(3);
    cell = row.createCell((short) 0);
    cell.setCellValue("Time Escaped");
    cell = row.createCell((short) 1);
    cell.setCellValue(Utility.toTime(time / 1000));
    row = sheet.createRow(5);
    row.createCell((short) 0).setCellValue("Rank");
    row.createCell((short) 1).setCellValue("Handle");
    row.createCell((short) 2).setCellValue("Nickname");
    row.createCell((short) 3).setCellValue("Solved");
    short columnIndex = 4;
    for (Problem problem2 : problems) {
        Problem problem = problem2;
        row.createCell(columnIndex).setCellValue(problem.getCode());
        columnIndex++;
    }
    row.createCell(columnIndex).setCellValue("Penalty");
    int rowIndex = 6;
    for (RankListEntry rankListEntry : entries) {
        RankListEntry entry = rankListEntry;
        row = sheet.createRow(rowIndex);
        row.createCell((short) 0).setCellValue(rowIndex - 5);
        row.createCell((short) 1).setCellValue(entry.getUserProfile().getHandle());
        String nick = entry.getUserProfile().getHandle();
        if (entry.getUserProfile().getNickName() != null) {
            nick = entry.getUserProfile().getNickName();
        }
        cell = row.createCell((short) 2);
        cell.setEncoding(HSSFCell.ENCODING_UTF_16);
        cell.setCellValue(nick);
        row.createCell((short) 3).setCellValue(entry.getSolved());
        for (short i = 0; i < problems.size(); ++i) {
            String score = entry.getAcceptTime(i) > 0 ? entry.getAcceptTime(i) + "(" + entry.getSubmitNumber(i) + ")" : "" + entry.getSubmitNumber(i);
            row.createCell((short) (4 + i)).setCellValue(score);
        }
        row.createCell((short) (4 + problems.size())).setCellValue(entry.getPenalty());
        rowIndex++;
    }
    // output to stream
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        wb.write(out);
        return out.toByteArray();
    } finally {
        out.close();
    }
}
Also used : RankListEntry(cn.edu.zju.acm.onlinejudge.util.RankListEntry) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) Problem(cn.edu.zju.acm.onlinejudge.bean.Problem) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 54 with HSSFRow

use of org.apache.poi.hssf.usermodel.HSSFRow in project zoj by licheng.

the class UserSearchAction method exportToExcel.

private byte[] exportToExcel(UserCriteria criteria, List<UserProfile> users) throws Exception {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet();
    short rowId = 0;
    for (Object user : users) {
        HSSFRow row = sheet.createRow(rowId);
        rowId++;
        HSSFCell cell = row.createCell((short) 0);
        cell.setCellValue(((UserProfile) user).getHandle());
    }
    // output to stream
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        wb.write(out);
        return out.toByteArray();
    } finally {
        out.close();
    }
}
Also used : HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 55 with HSSFRow

use of org.apache.poi.hssf.usermodel.HSSFRow in project wechat by dllwh.

the class ExcelExportUtil method export.

/**
 * -------------------------- 私有方法 end -------------------------------
 */
/**
 * -------------------------- 公有方法 start -------------------------------
 */
/**
 * @Title: export
 * @Description: 基于结果集的导出
 *               <ul>
 *               <li>基于结果集的导出</li>
 *               <li>XSSFWorkbook used for .xslx >= 2007</li>
 *               <li>HSSFWorkbook used for .xsl 03</li>
 *               </ul>
 * @param title
 * @param cellNames
 * @param dbList
 * @return
 * @throws Exception
 */
public static <T> byte[] export(String title, String[][] cellNames, List<T> dbList) throws Exception {
    if (CollectionUtils.isEmpty(dbList) || dbList.size() < 0) {
        return null;
    }
    // 创建Excel的工作书册 Workbook,对应到一个excel文档
    HSSFWorkbook workbook = new HSSFWorkbook();
    Map<String, CellStyle> getStyle = createStyles(workbook);
    // 创建Excel的工作sheet,对应到一个excel文档的tab
    HSSFSheet sheet = workbook.createSheet(title);
    // 创建字体样式
    HSSFFont font = workbook.createFont();
    font.setFontName("宋体");
    // 创建单元格样式
    // 创建Excel的sheet的一行
    HSSFRow row = sheet.createRow(0);
    // 设定行的高度
    row.setHeight((short) 500);
    // 创建一个Excel的单元格
    HSSFCell cell = row.createCell(0);
    // 合并单元格(startRow,endRow,startColumn,endColumn)
    sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, cellNames.length - 1));
    // 给合并后的单元格加上样式
    for (int j = 0; j <= cellNames.length - 1; j++) {
        HSSFCell celTemp = row.getCell(j);
        if (celTemp == null) {
            celTemp = row.createCell(j);
        }
    }
    // 给Excel的单元格设置样式和赋值
    cell.setCellValue(title);
    HSSFRow rowTitle = sheet.createRow(1);
    for (int i = 0; i < cellNames.length; i++) {
        HSSFCell cellTitle = rowTitle.createCell(i);
        // 设置excel列名
        cellTitle.setCellValue(cellNames[i][1]);
    }
    int i = 0;
    for (T bd : dbList) {
        row = sheet.createRow(i + 2);
        for (int j = 0; j < cellNames.length; j++) {
            HSSFCell cellvalue = row.createCell(j);
            String value = null;
            Object object = ObjectUtils.getProperty(bd, cellNames[j][0]);
            if (null == object) {
                value = "";
            } else if (object instanceof Date) {
                Date date = (Date) object;
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                value = sdf.format(date);
            } else {
                value = String.valueOf(object);
            }
            if ("str".equals(cellNames[j][2])) {
                cellvalue.setCellValue(value);
                cellvalue.setCellStyle(getStyle.get("cell"));
            } else {
                HSSFDataFormat format = workbook.createDataFormat();
                HSSFCellStyle formatStyle = workbook.createCellStyle();
                // 设置边框
                formatStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
                formatStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
                formatStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
                formatStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
                // 设置字体
                formatStyle.setFont(font);
                if ("amt".equals(cellNames[j][2])) {
                    cellvalue.setCellValue(Double.parseDouble(value));
                    // 设置保留2位小数--使用Excel内嵌的格式
                    formatStyle.setDataFormat(format.getFormat("#,##0.00"));
                } else if ("datetime".equals(cellNames[j][2])) {
                    cellvalue.setCellValue(value);
                    // 设置日期格式--使用Excel内嵌的格式
                    formatStyle.setDataFormat(format.getFormat("yyyy-MM-dd hh:mm:ss"));
                }
                // 设置货币格式--使用自定义的格式
                // cellStyle.setDataFormat(format.getFormat("¥#,##0"));
                // 设置百分比格式--使用自定义的格式
                // cellStyle.setDataFormat(DataFormat.getBuiltinFormat("0.00%"));
                // 设置中文大写格式--使用自定义的格式
                // cellStyle.setDataFormat(format.getFormat("[DbNum2][$-804]0"));
                cellvalue.setCellStyle(formatStyle);
            }
        }
        i++;
    }
    if (i == 0) {
        return null;
    }
    for (int k = 0; k < cellNames.length; k++) {
        // 自动设置列宽
        sheet.autoSizeColumn(k, true);
    }
    // 将生成的Excel放入IO流中
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    // 在内存中把数据写入ByteArrayOutputStream os
    workbook.write(os);
    // 在内存中,得到os的字节数组
    byte[] content = os.toByteArray();
    return content;
}
Also used : HSSFDataFormat(org.apache.poi.hssf.usermodel.HSSFDataFormat) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Date(java.util.Date) HSSFCellStyle(org.apache.poi.hssf.usermodel.HSSFCellStyle) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFFont(org.apache.poi.hssf.usermodel.HSSFFont) HSSFCellStyle(org.apache.poi.hssf.usermodel.HSSFCellStyle) CellStyle(org.apache.poi.ss.usermodel.CellStyle) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

HSSFRow (org.apache.poi.hssf.usermodel.HSSFRow)124 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)98 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)82 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)71 HSSFCellStyle (org.apache.poi.hssf.usermodel.HSSFCellStyle)29 FileOutputStream (java.io.FileOutputStream)24 HSSFRichTextString (org.apache.poi.hssf.usermodel.HSSFRichTextString)24 Test (org.junit.Test)18 IOException (java.io.IOException)16 HSSFFormulaEvaluator (org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator)15 HSSFFont (org.apache.poi.hssf.usermodel.HSSFFont)14 File (java.io.File)13 ArrayList (java.util.ArrayList)12 CellValue (org.apache.poi.ss.usermodel.CellValue)10 OutputStream (java.io.OutputStream)8 HashMap (java.util.HashMap)8 Map (java.util.Map)7 AssertionFailedError (junit.framework.AssertionFailedError)7 FileInputStream (java.io.FileInputStream)6 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)6