Search in sources :

Example 31 with XSSFRow

use of org.apache.poi.xssf.usermodel.XSSFRow in project OpenRefine by OpenRefine.

the class XlsxExporterTests method exportSimpleXlsx.

@Test
public void exportSimpleXlsx() {
    CreateGrid(2, 2);
    try {
        SUT.export(project, options, engine, stream);
    } catch (IOException e) {
        Assert.fail();
    }
    ByteArrayInputStream inStream = new ByteArrayInputStream(stream.toByteArray());
    try {
        XSSFWorkbook wb = new XSSFWorkbook(inStream);
        XSSFSheet ws = wb.getSheetAt(0);
        XSSFRow row1 = ws.getRow(1);
        XSSFCell cell0 = row1.getCell(0);
        Assert.assertEquals(cell0.toString(), "row0cell0");
        wb.close();
    } catch (IOException e) {
        Assert.fail();
    }
}
Also used : XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) ByteArrayInputStream(java.io.ByteArrayInputStream) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) IOException(java.io.IOException) RefineTest(com.google.refine.RefineTest) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 32 with XSSFRow

use of org.apache.poi.xssf.usermodel.XSSFRow in project OpenRefine by OpenRefine.

the class XlsxExporterTests method exportXlsxDateType.

@Test
public void exportXlsxDateType() throws IOException {
    OffsetDateTime odt = OffsetDateTime.parse("2019-04-09T12:00+00:00");
    createDateGrid(2, 2, odt);
    try {
        SUT.export(project, options, engine, stream);
    } catch (IOException e) {
        Assert.fail();
    }
    ByteArrayInputStream inStream = new ByteArrayInputStream(stream.toByteArray());
    try {
        XSSFWorkbook wb = new XSSFWorkbook(inStream);
        XSSFSheet ws = wb.getSheetAt(0);
        XSSFRow row1 = ws.getRow(1);
        XSSFCell cell0 = row1.getCell(0);
        Assert.assertTrue(cell0.toString().contains("2019"));
        wb.close();
    } catch (IOException e) {
        Assert.fail();
    }
}
Also used : XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) OffsetDateTime(java.time.OffsetDateTime) ByteArrayInputStream(java.io.ByteArrayInputStream) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) IOException(java.io.IOException) RefineTest(com.google.refine.RefineTest) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 33 with XSSFRow

use of org.apache.poi.xssf.usermodel.XSSFRow in project mica2 by obiba.

the class ExcelContingencyWriter method writeTable.

private <T extends Number> void writeTable(XSSFSheet sheet, List<List<T>> tmp, String title, List<String> headers, List<String> values, boolean isContinuous) {
    writeTableHeaders(sheet, title, headers);
    ArrayList<String> rowHeaders = Lists.newArrayList(values);
    if (!isContinuous)
        rowHeaders.add("Total");
    int counter = 0;
    int rownum = 4;
    for (String k : rowHeaders) {
        int cellnum = 0;
        XSSFRow row = sheet.createRow(rownum++);
        XSSFCell cell = row.createCell(cellnum++);
        cell.setCellValue(k);
        cell.setCellStyle(headerStyle);
        for (T obj : tmp.get(counter)) {
            cell = row.createCell(cellnum++);
            cell.setCellStyle(tableStyle);
            if (obj instanceof Integer)
                cell.setCellValue((Integer) obj);
            else if (obj instanceof Float)
                cell.setCellValue((Float) obj);
            else
                cell.setCellValue(String.valueOf(obj));
        }
        counter++;
    }
    sheet.autoSizeColumn(0);
}
Also used : XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell)

Example 34 with XSSFRow

use of org.apache.poi.xssf.usermodel.XSSFRow in project translationstudio8 by heartsome.

the class ExportQAResult method beginExport.

public void beginExport(List<QAResultBean> dataList, List<String> fileLCList, IProgressMonitor monitor) {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    // 分成十份,其中解析文件 1 份,其余 9 份
    monitor.beginTask("", 10);
    monitor.setTaskName(Messages.getString("qa.export.ExportQAResult.monitor.title"));
    // 将 fileLCList 转换成相对路径
    List<File> fileList = new ArrayList<File>();
    for (String fileLC : fileLCList) {
        fileList.add(new File(fileLC));
    }
    for (IFile iFile : ResourceUtils.filesToIFiles(fileList)) {
        filePathList.add(iFile.getFullPath().toOSString());
    }
    if (monitor.isCanceled()) {
        throw new OperationCanceledException();
    }
    monitor.worked(1);
    // UNDO 这里按文件排序给注释了。。。。
    //		// 先按文件排序
    //		sort(dataList);
    // 工作簿
    XSSFWorkbook workbook = new XSSFWorkbook();
    // 创建sheet页
    XSSFSheet sheet = workbook.createSheet();
    // 设置sheet名称
    workbook.setSheetName(0, Messages.getString("qa.export.ExportQAResult.sheet.title"));
    sheet.setColumnWidth(0, 255 * 6);
    sheet.setColumnWidth(1, 255 * 20);
    sheet.setColumnWidth(2, 255 * 30);
    sheet.setColumnWidth(3, 255 * 60);
    sheet.setColumnWidth(4, 255 * 60);
    XSSFFont titleFont = workbook.createFont();
    titleFont.setColor(IndexedColors.GREY_80_PERCENT.getIndex());
    titleFont.setBold(true);
    titleFont.setFontHeight(20);
    XSSFFont headerFont = workbook.createFont();
    headerFont.setBold(true);
    headerFont.setFontHeight(14);
    XSSFFont errorFont = workbook.createFont();
    errorFont.setColor(IndexedColors.RED.getIndex());
    XSSFCellStyle titleStyle = workbook.createCellStyle();
    titleStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
    titleStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
    titleStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
    titleStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
    titleStyle.setAlignment(HorizontalAlignment.CENTER);
    titleStyle.setFont(titleFont);
    XSSFCellStyle headerStyle = workbook.createCellStyle();
    headerStyle.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
    headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    headerStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
    headerStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
    headerStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
    headerStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
    headerStyle.setFont(headerFont);
    XSSFCellStyle cellStyle = workbook.createCellStyle();
    cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
    cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
    cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
    cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
    cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
    cellStyle.setWrapText(true);
    XSSFCellStyle errorCellStyle = workbook.createCellStyle();
    errorCellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
    errorCellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
    errorCellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
    errorCellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
    errorCellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
    errorCellStyle.setWrapText(true);
    errorCellStyle.setFont(errorFont);
    // 生成标题行
    XSSFRow row = sheet.createRow(0);
    XSSFCell cell = row.createCell(0);
    cell.setCellStyle(titleStyle);
    cell.setCellValue(Messages.getString("qa.export.ExportQAResult.titleCell"));
    String[] headers = new String[] { // 级别
    Messages.getString("qa.export.ExportQAResult.header.errorLeavel"), // 类型
    Messages.getString("qa.export.ExportQAResult.header.qaType"), // 位置
    Messages.getString("qa.export.ExportQAResult.header.location"), // 源文
    Messages.getString("qa.export.ExportQAResult.header.srcText"), //  译文
    Messages.getString("qa.export.ExportQAResult.header.tgtText") };
    // 产生表格标题行
    row = sheet.createRow(1);
    for (short i = 0; i < headers.length; i++) {
        cell = row.createCell(i);
        cell.setCellStyle(headerStyle);
        cell.setCellValue(headers[i]);
    }
    // 开始生成数据
    int index = 1;
    String rowId = null;
    // 先处理品质检查结果数据为空的情况
    if (dataList.size() <= 0) {
        if (isMultiFile) {
            String multiFileStr = getMultiResouce();
            index++;
            row = sheet.createRow(index);
            for (int i = 0; i < headers.length; i++) {
                cell = row.createCell(i);
                cell.setCellStyle(cellStyle);
                if (i == headers.length - 1) {
                    cell.setCellValue(multiFileStr);
                }
            }
        } else {
            for (String filePath : this.filePathList) {
                index++;
                row = sheet.createRow(index);
                for (int i = 0; i < headers.length; i++) {
                    cell = row.createCell(i);
                    cell.setCellStyle(cellStyle);
                    if (i == headers.length - 1) {
                        cell.setCellValue(filePath);
                    }
                }
            }
        }
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 4));
    } else {
        int interval = 1;
        if (dataList.size() > 9) {
            interval = dataList.size() / 9;
        }
        int startMergeRow = -1;
        int endMergeRow = -1;
        for (int i = 0; i < dataList.size(); i++) {
            QAResultBean bean = dataList.get(i);
            index++;
            System.out.println(index);
            if (index % interval == 0) {
                if (monitor.isCanceled()) {
                    throw new OperationCanceledException();
                }
                monitor.worked(1);
            }
            row = sheet.createRow(index);
            // 处理合并 级别 与 类型 两列的行
            mergeIF: if (bean.getMergeId() != null) {
                if (mergedIdSet.contains(bean.getMergeId())) {
                    break mergeIF;
                }
                startMergeRow = index;
                mergeFor: for (int j = i + 1; j < dataList.size(); j++) {
                    if (dataList.get(j).getMergeId() != null && dataList.get(j).getMergeId().equals(bean.getMergeId())) {
                        mergedIdSet.add(bean.getMergeId());
                        endMergeRow = index + (j - i);
                    } else {
                        break mergeFor;
                    }
                }
                if (startMergeRow >= 0 && endMergeRow >= 0) {
                    sheet.addMergedRegion(new CellRangeAddress(startMergeRow, endMergeRow, 0, 0));
                    sheet.addMergedRegion(new CellRangeAddress(startMergeRow, endMergeRow, 1, 1));
                    startMergeRow = -1;
                    endMergeRow = -1;
                }
            }
            // 循环当前行的每一列
            for (int h = 0; h < headers.length; h++) {
                cell = row.createCell(h);
                cell.setCellStyle(cellStyle);
                String text = null;
                switch(h) {
                    case 0:
                        if (bean.getLevel() == 0) {
                            text = Messages.getString("qa.export.ExportQAResult.errorLeavel.error");
                            cell.setCellStyle(errorCellStyle);
                        } else if (bean.getLevel() == 1) {
                            text = Messages.getString("qa.export.ExportQAResult.errorLeavel.warning");
                        }
                        cell.setCellValue(text);
                        break;
                    case 1:
                        text = bean.getQaTypeText();
                        cell.setCellValue(text);
                        break;
                    case 2:
                        text = bean.getFileName() + " [" + bean.getLineNumber() + "]";
                        cell.setCellValue(text);
                        break;
                    case 3:
                        text = bean.getSrcContent();
                        cell.setCellValue(getDisplayText(text));
                        break;
                    case 4:
                        text = bean.getTgtContent();
                        cell.setCellValue(getDisplayText(text));
                        break;
                    default:
                        break;
                }
            }
        }
        //	            // 这是合并 文件路径
        //	            if (isMultiFile) {
        //	            	sheet.addMergedRegion(new CellRangeAddress(resourceIndex, index, 6, 6));
        //	    		}else {
        //	    			sheet.addMergedRegion(new CellRangeAddress(resourceIndex, index, 6, 6));
        //	    		}
        //	            sheet.addMergedRegion(new CellRangeAddress(rowidIndex, index, 1, 1));
        //	    		sheet.addMergedRegion(new CellRangeAddress(rowidIndex, index, 4, 4));
        //	    		sheet.addMergedRegion(new CellRangeAddress(rowidIndex, index, 5, 5));
        // 标题行合并(处理未合并完的部份)
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 4));
    }
    try {
        FileOutputStream fileoutputstream = new FileOutputStream(exportFilePath);
        workbook.write(fileoutputstream);
        fileoutputstream.close();
        Display.getDefault().syncExec(new Runnable() {

            public void run() {
                MessageDialog.openInformation(Display.getDefault().getActiveShell(), Messages.getString("qa.all.dialog.info"), Messages.getString("qa.export.ExportQAResult.MSG.exportSuccess"));
            }
        });
    } catch (Exception e) {
        Display.getDefault().syncExec(new Runnable() {

            public void run() {
                MessageDialog.openInformation(Display.getDefault().getActiveShell(), Messages.getString("qa.all.dialog.info"), Messages.getString("qa.export.ExportQAResult.MSG.exportFail"));
            }
        });
        LOGGER.error(Messages.getString("qa.export.ExportQAResult.LOG.exportError"), e);
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) QAResultBean(net.heartsome.cat.ts.ui.qa.model.QAResultBean) ArrayList(java.util.ArrayList) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFCellStyle(org.apache.poi.xssf.usermodel.XSSFCellStyle) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFFont(org.apache.poi.xssf.usermodel.XSSFFont) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 35 with XSSFRow

use of org.apache.poi.xssf.usermodel.XSSFRow in project poi by apache.

the class TestSXSSFWorkbookWithCustomZipEntrySource method customZipEntrySource.

// write an unencrypted workbook to disk, but any temporary files are encrypted
@Test
public void customZipEntrySource() throws IOException {
    SXSSFWorkbookWithCustomZipEntrySource workbook = new SXSSFWorkbookWithCustomZipEntrySource();
    SXSSFSheet sheet1 = workbook.createSheet(sheetName);
    SXSSFRow row1 = sheet1.createRow(1);
    SXSSFCell cell1 = row1.createCell(1);
    cell1.setCellValue(cellValue);
    ByteArrayOutputStream os = new ByteArrayOutputStream(8192);
    workbook.write(os);
    workbook.close();
    workbook.dispose();
    XSSFWorkbook xwb = new XSSFWorkbook(new ByteArrayInputStream(os.toByteArray()));
    XSSFSheet xs1 = xwb.getSheetAt(0);
    assertEquals(sheetName, xs1.getSheetName());
    XSSFRow xr1 = xs1.getRow(1);
    XSSFCell xc1 = xr1.getCell(1);
    assertEquals(cellValue, xc1.getStringCellValue());
    xwb.close();
}
Also used : SXSSFWorkbookWithCustomZipEntrySource(org.apache.poi.poifs.crypt.temp.SXSSFWorkbookWithCustomZipEntrySource) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) ByteArrayInputStream(java.io.ByteArrayInputStream) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

XSSFRow (org.apache.poi.xssf.usermodel.XSSFRow)59 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)39 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)39 XSSFCell (org.apache.poi.xssf.usermodel.XSSFCell)35 FileOutputStream (java.io.FileOutputStream)17 IOException (java.io.IOException)16 ArrayList (java.util.ArrayList)15 FileInputStream (java.io.FileInputStream)14 BufferedReader (java.io.BufferedReader)10 FileNotFoundException (java.io.FileNotFoundException)10 Test (org.junit.Test)10 InputStreamReader (java.io.InputStreamReader)9 Reader (java.io.Reader)9 File (java.io.File)8 HashMap (java.util.HashMap)8 XSSFCellStyle (org.apache.poi.xssf.usermodel.XSSFCellStyle)8 BufferedWriter (java.io.BufferedWriter)6 FileWriter (java.io.FileWriter)6 HashSet (java.util.HashSet)5 Map (java.util.Map)5