Java Code Examples for org.apache.poi.hssf.usermodel.HSSFRow#getFirstCellNum()
The following examples show how to use
org.apache.poi.hssf.usermodel.HSSFRow#getFirstCellNum() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: POIUtils.java From ermasterr with Apache License 2.0 | 6 votes |
public static Integer findColumn(final HSSFRow row, final String str) { for (int colNum = row.getFirstCellNum(); colNum <= row.getLastCellNum(); colNum++) { final HSSFCell cell = row.getCell(colNum); if (cell == null) { continue; } if (cell.getCellType() == Cell.CELL_TYPE_STRING) { final HSSFRichTextString cellValue = cell.getRichStringCellValue(); if (str.equals(cellValue.getString())) { return Integer.valueOf(colNum); } } } return null; }
Example 2
Source File: POIUtils.java From ermasterr with Apache License 2.0 | 6 votes |
public static Integer findMatchColumn(final HSSFRow row, final String str) { for (int colNum = row.getFirstCellNum(); colNum <= row.getLastCellNum(); colNum++) { final HSSFCell cell = row.getCell(colNum); if (cell == null) { continue; } if (cell.getCellType() != Cell.CELL_TYPE_STRING) { continue; } final HSSFRichTextString cellValue = cell.getRichStringCellValue(); if (cellValue.getString().matches(str)) { return Integer.valueOf(colNum); } } return null; }
Example 3
Source File: POIUtils.java From ermasterr with Apache License 2.0 | 6 votes |
public static void copyRow(final HSSFSheet oldSheet, final HSSFSheet newSheet, final int oldStartRowNum, final int oldEndRowNum, final int newStartRowNum) { final HSSFRow oldAboveRow = oldSheet.getRow(oldStartRowNum - 1); int newRowNum = newStartRowNum; for (int oldRowNum = oldStartRowNum; oldRowNum <= oldEndRowNum; oldRowNum++) { POIUtils.copyRow(oldSheet, newSheet, oldRowNum, newRowNum++); } final HSSFRow newTopRow = newSheet.getRow(newStartRowNum); if (oldAboveRow != null) { for (int colNum = newTopRow.getFirstCellNum(); colNum <= newTopRow.getLastCellNum(); colNum++) { final HSSFCell oldAboveCell = oldAboveRow.getCell(colNum); if (oldAboveCell != null) { final HSSFCell newTopCell = newTopRow.getCell(colNum); newTopCell.getCellStyle().setBorderTop(oldAboveCell.getCellStyle().getBorderBottom()); } } } }
Example 4
Source File: POIUtils.java From ermasterr with Apache License 2.0 | 6 votes |
public static List<HSSFCellStyle> copyCellStyle(final HSSFWorkbook workbook, final HSSFRow row) { final List<HSSFCellStyle> cellStyleList = new ArrayList<HSSFCellStyle>(); for (int colNum = row.getFirstCellNum(); colNum <= row.getLastCellNum(); colNum++) { final HSSFCell cell = row.getCell(colNum); if (cell != null) { final HSSFCellStyle style = cell.getCellStyle(); final HSSFCellStyle newCellStyle = copyCellStyle(workbook, style); cellStyleList.add(newCellStyle); } else { cellStyleList.add(null); } } return cellStyleList; }
Example 5
Source File: POIUtils.java From ermaster-b with Apache License 2.0 | 6 votes |
public static Integer findColumn(HSSFRow row, String str) { for (int colNum = row.getFirstCellNum(); colNum <= row.getLastCellNum(); colNum++) { HSSFCell cell = row.getCell(colNum); if (cell == null) { continue; } if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) { HSSFRichTextString cellValue = cell.getRichStringCellValue(); if (str.equals(cellValue.getString())) { return Integer.valueOf(colNum); } } } return null; }
Example 6
Source File: POIUtils.java From ermaster-b with Apache License 2.0 | 6 votes |
public static Integer findMatchColumn(HSSFRow row, String str) { for (int colNum = row.getFirstCellNum(); colNum <= row.getLastCellNum(); colNum++) { HSSFCell cell = row.getCell(colNum); if (cell == null) { continue; } if (cell.getCellType() != HSSFCell.CELL_TYPE_STRING) { continue; } HSSFRichTextString cellValue = cell.getRichStringCellValue(); if (cellValue.getString().matches(str)) { return Integer.valueOf(colNum); } } return null; }
Example 7
Source File: POIUtils.java From ermaster-b with Apache License 2.0 | 6 votes |
public static void copyRow(HSSFSheet oldSheet, HSSFSheet newSheet, int oldStartRowNum, int oldEndRowNum, int newStartRowNum) { HSSFRow oldAboveRow = oldSheet.getRow(oldStartRowNum - 1); int newRowNum = newStartRowNum; for (int oldRowNum = oldStartRowNum; oldRowNum <= oldEndRowNum; oldRowNum++) { POIUtils.copyRow(oldSheet, newSheet, oldRowNum, newRowNum++); } HSSFRow newTopRow = newSheet.getRow(newStartRowNum); if (oldAboveRow != null) { for (int colNum = newTopRow.getFirstCellNum(); colNum <= newTopRow .getLastCellNum(); colNum++) { HSSFCell oldAboveCell = oldAboveRow.getCell(colNum); if (oldAboveCell != null) { HSSFCell newTopCell = newTopRow.getCell(colNum); newTopCell.getCellStyle().setBorderTop( oldAboveCell.getCellStyle().getBorderBottom()); } } } }
Example 8
Source File: POIUtils.java From ermaster-b with Apache License 2.0 | 6 votes |
public static List<HSSFCellStyle> copyCellStyle(HSSFWorkbook workbook, HSSFRow row) { List<HSSFCellStyle> cellStyleList = new ArrayList<HSSFCellStyle>(); for (int colNum = row.getFirstCellNum(); colNum <= row.getLastCellNum(); colNum++) { HSSFCell cell = row.getCell(colNum); if (cell != null) { HSSFCellStyle style = cell.getCellStyle(); HSSFCellStyle newCellStyle = copyCellStyle(workbook, style); cellStyleList.add(newCellStyle); } else { cellStyleList.add(null); } } return cellStyleList; }
Example 9
Source File: WordEmbedsTest.java From sun-wordtable-read with Apache License 2.0 | 5 votes |
private static void listEmbeds2(XWPFDocument doc) throws Exception { for (final PackagePart pPart : doc.getAllEmbedds()) { final String contentType = pPart.getContentType(); System.out.println(contentType + "\n"); if (contentType.equals("application/vnd.ms-excel")) { final HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(pPart.getInputStream()); for (int sheet = 0; sheet < embeddedWorkbook.getNumberOfSheets(); sheet++) { final HSSFSheet activeSheet = embeddedWorkbook.getSheetAt(sheet); if (activeSheet.getSheetName().equalsIgnoreCase("Sheet1")) { for (int rowIndex = activeSheet.getFirstRowNum(); rowIndex <= activeSheet .getLastRowNum(); rowIndex++) { final HSSFRow row = activeSheet.getRow(rowIndex); for (int cellIndex = row.getFirstCellNum(); cellIndex <= row .getLastCellNum(); cellIndex++) { final HSSFCell cell = row.getCell(cellIndex); if (cell != null) { if (cell.getCellType() == Cell.CELL_TYPE_STRING) System.out.println("Row:" + rowIndex + " Cell:" + cellIndex + " = " + cell.getStringCellValue()); if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { System.out.println("Row:" + rowIndex + " Cell:" + cellIndex + " = " + cell.getNumericCellValue()); cell.setCellValue(cell.getNumericCellValue() * 2); // update // the // value } } } } } } } } }
Example 10
Source File: Util.java From Knowage-Server with GNU Affero General Public License v3.0 | 5 votes |
/** * @param source * the sheet to copy. * @param destSheet * the sheet to create. * @param srcRow * the row to copy. * @param destRow * the row to create. * @param styleMap * */ private static void copyRow(HSSFSheet source, XSSFSheet destSheet, HSSFRow srcRow, XSSFRow destRow, List<CellStyle> styleMap) { Set<CellRangeAddressWrapper> mergedRegions = new TreeSet<CellRangeAddressWrapper>(); short dh = source.getDefaultRowHeight(); if (srcRow.getHeight() != dh) { destRow.setHeight(srcRow.getHeight()); } int j = srcRow.getFirstCellNum(); if (j < 0) { j = 0; } for (; j <= srcRow.getLastCellNum(); j++) { HSSFCell oldCell = srcRow.getCell(j); XSSFCell newCell = destRow.getCell(j); if (oldCell != null) { if (newCell == null) { newCell = destRow.createCell(j); } copyCell(oldCell, newCell, styleMap); CellRangeAddress mergedRegion = getMergedRegion(source, srcRow.getRowNum(), (short) oldCell.getColumnIndex()); if (mergedRegion != null) { CellRangeAddress newMergedRegion = new CellRangeAddress(mergedRegion.getFirstRow(), mergedRegion.getLastRow(), mergedRegion.getFirstColumn(), mergedRegion.getLastColumn()); CellRangeAddressWrapper wrapper = new CellRangeAddressWrapper(newMergedRegion); if (isNewMergedRegion(wrapper, mergedRegions)) { mergedRegions.add(wrapper); destSheet.addMergedRegion(wrapper.range); } } } } }
Example 11
Source File: ExcelHandle.java From danyuan-application with Apache License 2.0 | 4 votes |
@SuppressWarnings({ "resource", "deprecation" }) public List<String[]> readXLS(String path, int num) throws FileNotFoundException, IOException { File file = new File(path); POIFSFileSystem poifsFileSystem = new POIFSFileSystem(new FileInputStream(file)); HSSFWorkbook hssfWorkbook = new HSSFWorkbook(poifsFileSystem); HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(num); int rowstart = hssfSheet.getFirstRowNum(); int rowEnd = hssfSheet.getLastRowNum(); for (int i = rowstart; i <= rowEnd; i++) { HSSFRow row = hssfSheet.getRow(i); if (null == row) { continue; } int cellStart = row.getFirstCellNum(); int cellEnd = row.getLastCellNum(); for (int k = cellStart; k <= cellEnd; k++) { HSSFCell cell = row.getCell(k); if (null == cell) { continue; } System.out.print("" + k + " "); // System.out.print("type:"+cell.getCellType()); switch (cell.getCellTypeEnum()) { case NUMERIC: // 数字 System.out.print(cell.getNumericCellValue() + " "); break; case STRING: // 字符串 System.out.print(cell.getStringCellValue() + " "); break; case BOOLEAN: // Boolean System.out.println(cell.getBooleanCellValue() + " "); break; case FORMULA: // 公式 System.out.print(cell.getCellFormula() + " "); break; case BLANK: // 空值 System.out.println(" "); break; case ERROR: // 故障 System.out.println(" "); break; default: System.out.print("未知类型 "); break; } } System.out.print("\n"); } return null; }
Example 12
Source File: OfficeConverter.java From BBSSDK-for-Android with Apache License 2.0 | 4 votes |
private void xlsToHtml() throws Throwable { FileOutputStream output = new FileOutputStream(new File(htmlPath)); StringBuffer htmlHeaderSB = new StringBuffer(); htmlHeaderSB.append("<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' " + "xmlns='http://www.w3.org/TR/REC-html40'>"); htmlHeaderSB.append("<head><meta http-equiv=Content-Type content='text/html; charset=utf-8'><meta name=ProgId content=Excel.Sheet>" + "</head><body>"); output.write(htmlHeaderSB.toString().getBytes()); HSSFSheet sheet; HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filePath)); // 获整个Excel for (int sheetIndex = 0; sheetIndex < workbook.getNumberOfSheets(); sheetIndex++) { if (workbook.getSheetAt(sheetIndex) != null) { sheet = workbook.getSheetAt(sheetIndex);// 获得不为空的这个sheet if (sheet != null) { int firstRowNum = sheet.getFirstRowNum(); // 第一行 int lastRowNum = sheet.getLastRowNum(); // 最后一行 // 构造Table output.write(("<table width=\"100%\" style=\"border:1px solid #000;border-width:1px 0 0 1px;margin:2px 0 2px 0;" + "border-collapse:collapse;\">").getBytes()); for (int rowNum = firstRowNum; rowNum <= lastRowNum; rowNum++) { if (sheet.getRow(rowNum) != null) {// 如果行不为空, HSSFRow row = sheet.getRow(rowNum); short firstCellNum = row.getFirstCellNum(); // 该行的第一个单元格 short lastCellNum = row.getLastCellNum(); // 该行的最后一个单元格 int height = (int) (row.getHeight() / 15.625); // 行的高度 output.write(("<tr height=\"" + height + "\" style=\"border:1px solid #000;border-width:0 1px 1px 0;" + "margin:2px 0 2px 0;\">").getBytes()); for (short cellNum = firstCellNum; cellNum <= lastCellNum; cellNum++) { // 循环该行的每一个单元格 HSSFCell cell = row.getCell(cellNum); if (cell != null) { if (cell.getCellType() != HSSFCell.CELL_TYPE_BLANK) { StringBuffer tdStyle = new StringBuffer("<td style=\"border:1px solid #000; border-width:0 1px 1px 0;" + "margin:2px 0 2px 0; "); HSSFCellStyle cellStyle = cell.getCellStyle(); HSSFPalette palette = workbook.getCustomPalette(); // 类HSSFPalette用于求颜色的国际标准形式 HSSFColor hColor = palette.getColor(cellStyle.getFillForegroundColor()); HSSFColor hColor2 = palette.getColor(cellStyle.getFont(workbook).getColor()); String bgColor = convertToStardColor(hColor);// 背景颜色 short boldWeight = cellStyle.getFont(workbook).getBoldweight(); // 字体粗细 short fontHeight = (short) (cellStyle.getFont(workbook).getFontHeight() / 2); // 字体大小 String fontColor = convertToStardColor(hColor2); // 字体颜色 if (bgColor != null && !"".equals(bgColor.trim())) { tdStyle.append(" background-color:"); tdStyle.append(bgColor); tdStyle.append("; "); } if (fontColor != null && !"".equals(fontColor.trim())) { tdStyle.append(" color:"); tdStyle.append(fontColor); tdStyle.append("; "); } tdStyle.append(" font-weight:"); tdStyle.append(boldWeight); tdStyle.append("; "); tdStyle.append(" font-size: "); tdStyle.append(fontHeight); tdStyle.append("%;"); output.write((tdStyle + "\"").getBytes()); int width = (int) (sheet.getColumnWidth(cellNum) / 35.7); // int cellRegionCol = getMergerCellRegionCol(sheet, rowNum, cellNum); // 合并的列(solspan) int cellRegionRow = getMergerCellRegionRow(sheet, rowNum, cellNum);// 合并的行(rowspan) String align = convertAlignToHtml(cellStyle.getAlignment()); // String vAlign = convertVerticalAlignToHtml(cellStyle.getVerticalAlignment()); output.write((" align=\"" + align + "\" valign=\"" + vAlign + "\" width=\"" + width + "\" ").getBytes()); output.write((" colspan=\"" + cellRegionCol + "\" rowspan=\"" + cellRegionRow + "\"").getBytes()); output.write((">" + getCellValue(cell) + "</td>").getBytes()); } } } output.write("</tr>".getBytes()); } } output.write(("</table>").getBytes()); } } } output.write(("</body></html>").getBytes()); output.close(); }
Example 13
Source File: POIUtils.java From ermasterr with Apache License 2.0 | 4 votes |
public static void copyRow(final HSSFSheet oldSheet, final HSSFSheet newSheet, final int oldRowNum, final int newRowNum) { final HSSFRow oldRow = oldSheet.getRow(oldRowNum); final HSSFRow newRow = newSheet.createRow(newRowNum); if (oldRow == null) { return; } newRow.setHeight(oldRow.getHeight()); if (oldRow.getFirstCellNum() == -1) { return; } for (int colNum = oldRow.getFirstCellNum(); colNum <= oldRow.getLastCellNum(); colNum++) { final HSSFCell oldCell = oldRow.getCell(colNum); final HSSFCell newCell = newRow.createCell(colNum); if (oldCell != null) { final HSSFCellStyle style = oldCell.getCellStyle(); newCell.setCellStyle(style); final int cellType = oldCell.getCellType(); newCell.setCellType(cellType); if (cellType == Cell.CELL_TYPE_BOOLEAN) { newCell.setCellValue(oldCell.getBooleanCellValue()); } else if (cellType == Cell.CELL_TYPE_FORMULA) { newCell.setCellFormula(oldCell.getCellFormula()); } else if (cellType == Cell.CELL_TYPE_NUMERIC) { newCell.setCellValue(oldCell.getNumericCellValue()); } else if (cellType == Cell.CELL_TYPE_STRING) { newCell.setCellValue(oldCell.getRichStringCellValue()); } } } POIUtils.copyMergedRegion(newSheet, getMergedRegionList(oldSheet, oldRowNum), newRowNum); }
Example 14
Source File: AbstractSheetGenerator.java From ermasterr with Apache License 2.0 | 4 votes |
protected ColumnTemplate loadColumnTemplate(final HSSFWorkbook workbook, final HSSFSheet templateSheet, final CellLocation location) { if (location == null) { return null; } final ColumnTemplate columnTemplate = new ColumnTemplate(); final HSSFRow row = templateSheet.getRow(location.r); final HSSFRow bottomRow = templateSheet.getRow(location.r + 1); for (int colNum = row.getFirstCellNum(); colNum <= row.getLastCellNum(); colNum++) { final HSSFCell cell = row.getCell(colNum); if (cell != null) { columnTemplate.columnTemplateMap.put(colNum, cell.getRichStringCellValue().getString()); } } columnTemplate.topRowCellStyleList = POIUtils.copyCellStyle(workbook, row); columnTemplate.middleRowCellStyleList = POIUtils.copyCellStyle(workbook, row); columnTemplate.bottomRowCellStyleList = POIUtils.copyCellStyle(workbook, row); for (short i = 0; i < columnTemplate.middleRowCellStyleList.size(); i++) { final HSSFCellStyle middleRowCellStyle = columnTemplate.middleRowCellStyleList.get(i); if (middleRowCellStyle != null) { final HSSFCellStyle topRowCellStyle = columnTemplate.topRowCellStyleList.get(i); final HSSFCellStyle bottomRowCellStyle = columnTemplate.bottomRowCellStyleList.get(i); final HSSFCell bottomCell = bottomRow.getCell(row.getFirstCellNum() + i); topRowCellStyle.setBorderBottom(bottomCell.getCellStyle().getBorderTop()); middleRowCellStyle.setBorderTop(bottomCell.getCellStyle().getBorderTop()); middleRowCellStyle.setBorderBottom(bottomCell.getCellStyle().getBorderTop()); bottomRowCellStyle.setBorderTop(bottomCell.getCellStyle().getBorderTop()); bottomRowCellStyle.setBorderBottom(bottomCell.getCellStyle().getBorderBottom()); } } return columnTemplate; }
Example 15
Source File: POIUtils.java From ermaster-b with Apache License 2.0 | 4 votes |
public static void copyRow(HSSFSheet oldSheet, HSSFSheet newSheet, int oldRowNum, int newRowNum) { HSSFRow oldRow = oldSheet.getRow(oldRowNum); HSSFRow newRow = newSheet.createRow(newRowNum); if (oldRow == null) { return; } newRow.setHeight(oldRow.getHeight()); if (oldRow.getFirstCellNum() == -1) { return; } for (int colNum = oldRow.getFirstCellNum(); colNum <= oldRow .getLastCellNum(); colNum++) { HSSFCell oldCell = oldRow.getCell(colNum); HSSFCell newCell = newRow.createCell(colNum); if (oldCell != null) { HSSFCellStyle style = oldCell.getCellStyle(); newCell.setCellStyle(style); int cellType = oldCell.getCellType(); newCell.setCellType(cellType); if (cellType == HSSFCell.CELL_TYPE_BOOLEAN) { newCell.setCellValue(oldCell.getBooleanCellValue()); } else if (cellType == HSSFCell.CELL_TYPE_FORMULA) { newCell.setCellFormula(oldCell.getCellFormula()); } else if (cellType == HSSFCell.CELL_TYPE_NUMERIC) { newCell.setCellValue(oldCell.getNumericCellValue()); } else if (cellType == HSSFCell.CELL_TYPE_STRING) { newCell.setCellValue(oldCell.getRichStringCellValue()); } } } POIUtils.copyMergedRegion(newSheet, getMergedRegionList(oldSheet, oldRowNum), newRowNum); }
Example 16
Source File: AbstractSheetGenerator.java From ermaster-b with Apache License 2.0 | 4 votes |
protected ColumnTemplate loadColumnTemplate(HSSFWorkbook workbook, HSSFSheet templateSheet, CellLocation location) { if (location == null) { return null; } ColumnTemplate columnTemplate = new ColumnTemplate(); HSSFRow row = templateSheet.getRow(location.r); HSSFRow bottomRow = templateSheet.getRow(location.r + 1); for (int colNum = row.getFirstCellNum(); colNum <= row.getLastCellNum(); colNum++) { HSSFCell cell = row.getCell(colNum); if (cell != null) { columnTemplate.columnTemplateMap.put(colNum, cell .getRichStringCellValue().getString()); } } columnTemplate.topRowCellStyleList = POIUtils.copyCellStyle(workbook, row); columnTemplate.middleRowCellStyleList = POIUtils.copyCellStyle( workbook, row); columnTemplate.bottomRowCellStyleList = POIUtils.copyCellStyle( workbook, row); for (short i = 0; i < columnTemplate.middleRowCellStyleList.size(); i++) { HSSFCellStyle middleRowCellStyle = columnTemplate.middleRowCellStyleList .get(i); if (middleRowCellStyle != null) { HSSFCellStyle topRowCellStyle = columnTemplate.topRowCellStyleList .get(i); HSSFCellStyle bottomRowCellStyle = columnTemplate.bottomRowCellStyleList .get(i); HSSFCell bottomCell = bottomRow.getCell(row.getFirstCellNum() + i); topRowCellStyle.setBorderBottom(bottomCell.getCellStyle() .getBorderTop()); middleRowCellStyle.setBorderTop(bottomCell.getCellStyle() .getBorderTop()); middleRowCellStyle.setBorderBottom(bottomCell.getCellStyle() .getBorderTop()); bottomRowCellStyle.setBorderTop(bottomCell.getCellStyle() .getBorderTop()); bottomRowCellStyle.setBorderBottom(bottomCell.getCellStyle() .getBorderBottom()); } } return columnTemplate; }