Search in sources :

Example 11 with XSSFRow

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

the class ExcelUtil method readExcelCell07.

private static Object[][] readExcelCell07(Workbook wb, String sheetName, int rowNum, int columnNum) throws Exception {
    Object[][] result = null;
    int cellType = 0;
    XSSFRow row = null;
    XSSFCell cell = null;
    result = new Object[rowNum][columnNum];
    XSSFSheet sheet = (XSSFSheet) wb.getSheet(sheetName);
    // 开始循环遍历单元格,取出数据放入result的二维数组中
    for (int i = START_ROW; i < rowNum; i++) {
        row = sheet.getRow(i);
        cellType = -1;
        Object cellValue = null;
        // 确保此行有数据
        if (row == null) {
            result[i] = null;
            continue;
        }
        boolean rowEmptyFlg = true;
        for (int j = 0; j < columnNum; j++) {
            cell = row.getCell(j);
            if (cell != null) {
                rowEmptyFlg = false;
                // 判断单元格内数据类型,
                try {
                    // 數字型必須要先檢測,他既不會走if也不會走catch
                    cellType = cell.getCellType();
                    if (DateUtil.isCellDateFormatted(cell)) {
                        // 日期格式需要这样来判断,下面的方法判断不了
                        cellType = CELL_TYPE_DATE;
                    }
                } catch (IllegalStateException e) {
                    cellType = cell.getCellType();
                }
                if (cellType == CELL_TYPE_NULL) {
                    // 空值型
                    result[i][j] = null;
                    continue;
                } else if (cellType == CELL_TYPE_NUM) {
                    // 数值型,避免科学计数法
                    DecimalFormat df = new DecimalFormat("0");
                    cellValue = df.format(cell.getNumericCellValue());
                } else if (cellType == CEll_TYPE_STRING) {
                    // 字符串型
                    cellValue = cell.getStringCellValue();
                } else if (cellType == CELL_TYPE_BOOLEAN) {
                    // boolean型
                    cellValue = new Boolean(cell.getBooleanCellValue());
                } else if (cellType == CELL_TYPE_DATE) {
                    // 日期类型
                    double value = cell.getNumericCellValue();
                    cellValue = DateUtil.getJavaDate(value);
                } else if (cellType == CEll_TYPE_EXPRESSION) {
                    cellValue = cell.getCTCell().getV();
                // cellValue = cell.getNumericCellValue();
                }
                result[i][j] = cellValue;
            } else {
                result[i][j] = null;
            }
        }
        // 如何该行每一列都没有数据,则该行为空
        if (rowEmptyFlg) {
            result[i] = null;
        }
    }
    return result;
}
Also used : XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) DecimalFormat(java.text.DecimalFormat) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell)

Example 12 with XSSFRow

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

the class ExcelNodeSerializer method startWriteRootNode.

@Override
protected void startWriteRootNode(RootNode rootNode) throws Exception {
    XSSFCreationHelper creationHelper = workbook.getCreationHelper();
    int rowIdx = 1;
    for (Node collectionNode : rootNode.getChildren()) {
        if (collectionNode.isCollection()) {
            for (Node complexNode : collectionNode.getChildren()) {
                XSSFRow row = sheet.createRow(rowIdx++);
                int cellIdx = 0;
                for (Node node : complexNode.getChildren()) {
                    if (node.isSimple()) {
                        XSSFCell cell = row.createCell(cellIdx++);
                        cell.setCellValue(getValue((SimpleNode) node));
                        if (node.haveProperty() && PropertyType.URL.equals(node.getProperty().getPropertyType())) {
                            XSSFHyperlink hyperlink = creationHelper.createHyperlink(HyperlinkType.URL);
                            hyperlink.setAddress(getValue((SimpleNode) node));
                            hyperlink.setLabel(getValue((SimpleNode) node));
                            cell.setHyperlink(hyperlink);
                        } else if (node.haveProperty() && PropertyType.EMAIL.equals(node.getProperty().getPropertyType())) {
                            XSSFHyperlink hyperlink = creationHelper.createHyperlink(HyperlinkType.EMAIL);
                            hyperlink.setAddress(getValue((SimpleNode) node));
                            hyperlink.setLabel(getValue((SimpleNode) node));
                            cell.setHyperlink(hyperlink);
                        }
                    }
                }
            }
        }
    }
}
Also used : XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFCreationHelper(org.apache.poi.xssf.usermodel.XSSFCreationHelper) CollectionNode(org.hisp.dhis.node.types.CollectionNode) Node(org.hisp.dhis.node.Node) SimpleNode(org.hisp.dhis.node.types.SimpleNode) ComplexNode(org.hisp.dhis.node.types.ComplexNode) RootNode(org.hisp.dhis.node.types.RootNode) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) SimpleNode(org.hisp.dhis.node.types.SimpleNode) XSSFHyperlink(org.apache.poi.xssf.usermodel.XSSFHyperlink)

Example 13 with XSSFRow

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

the class ExcelNodeSerializer method startSerialize.

@Override
protected void startSerialize(RootNode rootNode, OutputStream outputStream) throws Exception {
    workbook = new XSSFWorkbook();
    sheet = workbook.createSheet("Sheet1");
    XSSFFont boldFont = workbook.createFont();
    boldFont.setBold(true);
    XSSFCellStyle boldCellStyle = workbook.createCellStyle();
    boldCellStyle.setFont(boldFont);
    // build schema
    for (Node child : rootNode.getChildren()) {
        if (child.isCollection()) {
            if (!child.getChildren().isEmpty()) {
                Node node = child.getChildren().get(0);
                XSSFRow row = sheet.createRow(0);
                int cellIdx = 0;
                for (Node property : node.getChildren()) {
                    if (property.isSimple()) {
                        XSSFCell cell = row.createCell(cellIdx++);
                        cell.setCellValue(property.getName());
                        cell.setCellStyle(boldCellStyle);
                    }
                }
            }
        }
    }
}
Also used : XSSFCellStyle(org.apache.poi.xssf.usermodel.XSSFCellStyle) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFFont(org.apache.poi.xssf.usermodel.XSSFFont) CollectionNode(org.hisp.dhis.node.types.CollectionNode) Node(org.hisp.dhis.node.Node) SimpleNode(org.hisp.dhis.node.types.SimpleNode) ComplexNode(org.hisp.dhis.node.types.ComplexNode) RootNode(org.hisp.dhis.node.types.RootNode) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell)

Example 14 with XSSFRow

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

the class OfficeUtils method main.

public static void main(String[] args) {
    File file = new File("temp" + File.separator + "office" + File.separator + "maidian.xlsx");
    XSSFWorkbook xssfWorkbook = openXlsx(file);
    XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
    int totalRows = xssfSheet.getPhysicalNumberOfRows();
    for (int rowNum = 2; rowNum < totalRows; rowNum++) {
        XSSFRow row = xssfSheet.getRow(rowNum);
        int cellNum = row.getLastCellNum();
        if (cellNum == 6) {
            String name = getCellString(row.getCell(4));
            String eventid = getCellString(row.getCell(5));
            //                /**
            //                 * aa
            //                 */
            //                public static final String a = "a";
            System.out.println("/**");
            System.out.println(" * " + name);
            System.out.println(" */");
            System.out.println("public static final String " + eventid.toUpperCase() + " = \"" + eventid + "\"");
            System.out.println();
        }
    }
}
Also used : XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) File(java.io.File)

Example 15 with XSSFRow

use of org.apache.poi.xssf.usermodel.XSSFRow in project data-prep by Talend.

the class ExcelComparator method compareTwoSheets.

// Compare Two Sheets
public static boolean compareTwoSheets(XSSFSheet sheet1, XSSFSheet sheet2) {
    int firstRow1 = sheet1.getFirstRowNum();
    int lastRow1 = sheet1.getLastRowNum();
    boolean equalSheets = true;
    for (int i = firstRow1; i <= lastRow1; i++) {
        XSSFRow row1 = sheet1.getRow(i);
        XSSFRow row2 = sheet2.getRow(i);
        if (!compareTwoRows(row1, row2)) {
            equalSheets = false;
            break;
        }
    }
    return equalSheets;
}
Also used : XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow)

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