Java Code Examples for org.apache.poi.hssf.usermodel.HSSFWorkbook#getNumberOfSheets()
The following examples show how to use
org.apache.poi.hssf.usermodel.HSSFWorkbook#getNumberOfSheets() .
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: ExportToExcelManager.java From ermaster-b with Apache License 2.0 | 6 votes |
private int countSheetFromTemplate(HSSFWorkbook workbook, ERDiagram diagram) { int count = 0; for (int sheetNo = 0; sheetNo < workbook.getNumberOfSheets(); sheetNo++) { String templateSheetName = workbook.getSheetName(sheetNo); AbstractSheetGenerator sheetGenerator = this .getSheetGenerator(templateSheetName); if (sheetGenerator != null) { count += sheetGenerator.count(diagram); } else { count++; } } return count; }
Example 2
Source File: FileUtil.java From JavaWeb with Apache License 2.0 | 6 votes |
public static void readExcel(String filePth) throws Exception { InputStream is = new FileInputStream(filePth); //创建工作薄 //XSSFWorkbook hwb = new XSSFWorkbook(is); HSSFWorkbook hwb = new HSSFWorkbook(new POIFSFileSystem(is)); //得到sheet for (int i = 0; i < hwb.getNumberOfSheets(); i++) { HSSFSheet sheet = hwb.getSheetAt(i); int rows = sheet.getPhysicalNumberOfRows(); //遍历每一行 for (int j = 0; j < rows; j++) { HSSFRow hr = sheet.getRow(j); Iterator<?> it = hr.iterator(); while(it.hasNext()){ String context = it.next().toString(); System.out.println(context); } } } hwb.close(); }
Example 3
Source File: FileUtil.java From JavaWeb with Apache License 2.0 | 6 votes |
public static void readExcel(String filePth) throws Exception { InputStream is = new FileInputStream(filePth); //创建工作薄 //XSSFWorkbook hwb = new XSSFWorkbook(is); HSSFWorkbook hwb = new HSSFWorkbook(new POIFSFileSystem(is)); //得到sheet for (int i = 0; i < hwb.getNumberOfSheets(); i++) { HSSFSheet sheet = hwb.getSheetAt(i); int rows = sheet.getPhysicalNumberOfRows(); //遍历每一行 for (int j = 0; j < rows; j++) { HSSFRow hr = sheet.getRow(j); Iterator<?> it = hr.iterator(); while(it.hasNext()){ String context = it.next().toString(); System.out.println(context); } } } hwb.close(); }
Example 4
Source File: ExportToExcelManager.java From ermasterr with Apache License 2.0 | 6 votes |
private int countSheetFromTemplate(final HSSFWorkbook workbook, final ERDiagram diagram) { int count = 0; for (int sheetNo = 0; sheetNo < workbook.getNumberOfSheets(); sheetNo++) { final String templateSheetName = workbook.getSheetName(sheetNo); final AbstractSheetGenerator sheetGenerator = getSheetGenerator(templateSheetName); if (sheetGenerator != null) { count += sheetGenerator.count(diagram); } else { count++; } } if (exportExcelSetting.isPutERDiagramOnExcel()) { count += 1; } return count; }
Example 5
Source File: EsvExcelReaderImpl.java From cia with Apache License 2.0 | 6 votes |
@Override public Map<Integer, List<GovernmentBodyAnnualSummary>> getDataPerMinistry(final String name) { final Map<Integer, List<GovernmentBodyAnnualSummary>> map = new TreeMap<>(); try { final HSSFWorkbook myWorkBook = createGovermentBodyWorkBook(); for (int sheetNr = 0; sheetNr < myWorkBook.getNumberOfSheets(); sheetNr++) { addMinistryPerYearToMap(name, map, myWorkBook.getSheetAt(sheetNr)); } myWorkBook.close(); } catch (final IOException e) { LOGGER.warn("Problem loading", e); } return map; }
Example 6
Source File: EsvExcelReaderImpl.java From cia with Apache License 2.0 | 6 votes |
@Override public Map<Integer, GovernmentBodyAnnualSummary> getDataPerGovernmentBody(final String name) { final Map<Integer, GovernmentBodyAnnualSummary> map = new TreeMap<>(); try { final HSSFWorkbook myWorkBook = createGovermentBodyWorkBook(); for (int sheetNr = 0; sheetNr < myWorkBook.getNumberOfSheets(); sheetNr++) { final HSSFSheet mySheet = myWorkBook.getSheetAt(sheetNr); addDataForYearToMap(name, map, mySheet); } myWorkBook.close(); } catch ( final IOException e) { LOGGER.warn("Problem loading", e); } return map; }
Example 7
Source File: ExcelTopicNameExtractor.java From wandora with GNU General Public License v3.0 | 5 votes |
@Override public void processWorkbook(HSSFWorkbook workbook, TopicMap topicMap) { int numberOfSheets = workbook.getNumberOfSheets(); for(int i=0; i<numberOfSheets && !forceStop(); i++) { HSSFSheet sheet = workbook.getSheetAt(i); processSheet(sheet, topicMap); } }
Example 8
Source File: ExcelTopicExtractor.java From wandora with GNU General Public License v3.0 | 5 votes |
@Override public void processWorkbook(HSSFWorkbook workbook, TopicMap topicMap) { int numberOfSheets = workbook.getNumberOfSheets(); for(int i=0; i<numberOfSheets && !forceStop(); i++) { HSSFSheet sheet = workbook.getSheetAt(i); processSheet(sheet, topicMap); } }
Example 9
Source File: ExcelAdjacencyListExtractor.java From wandora with GNU General Public License v3.0 | 5 votes |
@Override public void processWorkbook(HSSFWorkbook workbook, TopicMap topicMap) { int numberOfSheets = workbook.getNumberOfSheets(); for(int i=0; i<numberOfSheets && !forceStop(); i++) { HSSFSheet sheet = workbook.getSheetAt(i); processSheet(sheet, topicMap); } }
Example 10
Source File: ExcelAdjacencyMatrixExtractor.java From wandora with GNU General Public License v3.0 | 5 votes |
@Override public void processWorkbook(HSSFWorkbook workbook, TopicMap topicMap) { int numberOfSheets = workbook.getNumberOfSheets(); for(int i=0; i<numberOfSheets && !forceStop(); i++) { HSSFSheet sheet = workbook.getSheetAt(i); processSheet(sheet, topicMap); } }
Example 11
Source File: ExcelTopicOccurrenceExtractor.java From wandora with GNU General Public License v3.0 | 5 votes |
@Override public void processWorkbook(HSSFWorkbook workbook, TopicMap topicMap) { int numberOfSheets = workbook.getNumberOfSheets(); for(int i=0; i<numberOfSheets && !forceStop(); i++) { HSSFSheet sheet = workbook.getSheetAt(i); processSheet(sheet, topicMap); } }
Example 12
Source File: ExcelTopicTreeExtractor.java From wandora with GNU General Public License v3.0 | 5 votes |
@Override public void processWorkbook(HSSFWorkbook workbook, TopicMap topicMap) { int numberOfSheets = workbook.getNumberOfSheets(); for(int i=0; i<numberOfSheets && !forceStop(); i++) { HSSFSheet sheet = workbook.getSheetAt(i); processSheet(sheet, topicMap); } }
Example 13
Source File: ExcelExtractor.java From wandora with GNU General Public License v3.0 | 5 votes |
public Collection getSheets(HSSFWorkbook workbook) { ArrayList<String> sheets = new ArrayList(); int numberOfSheets = workbook.getNumberOfSheets(); for(int i=0; i<numberOfSheets && !forceStop(); i++) { sheets.add(workbook.getSheetName(i)); } return sheets; }
Example 14
Source File: XlsDataSet.java From Leo with Apache License 2.0 | 5 votes |
/** * Creates a new XlsDataSet object that loads the specified Excel document. */ public XlsDataSet(InputStream in) throws IOException, DataSetException { _tables = super.createTableNameMap(); HSSFWorkbook workbook = new HSSFWorkbook(in); int sheetCount = workbook.getNumberOfSheets(); for (int i = 0; i < sheetCount; i++) { ITable table = new XlsTable(workbook, workbook.getSheetName(i), workbook.getSheetAt(i)); _tables.add(table.getTableMetaData().getTableName(), table); } }
Example 15
Source File: XlsDataSet.java From Leo with Apache License 2.0 | 5 votes |
/** * 从Excel的工作簿中创建XlsDataSet. */ @Deprecated public XlsDataSet(HSSFWorkbook workbook) throws IOException, DataSetException { _tables = super.createTableNameMap(); int sheetCount = workbook.getNumberOfSheets(); for (int i = 0; i < sheetCount; i++) { ITable table = new XlsTable(workbook, workbook.getSheetName(i), workbook.getSheetAt(i)); _tables.add(table.getTableMetaData().getTableName(), table); } }
Example 16
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 17
Source File: ExportToExcelManager.java From ermaster-b with Apache License 2.0 | 4 votes |
private void createSheetFromTemplate(IProgressMonitor monitor, HSSFWorkbook workbook, ERDiagram diagram, boolean useLogicalNameAsSheetName) throws InterruptedException { int originalSheetNum = workbook.getNumberOfSheets(); int sheetIndexSheetNo = -1; while (originalSheetNum > 0) { String templateSheetName = workbook.getSheetName(0); AbstractSheetGenerator sheetGenerator = this .getSheetGenerator(templateSheetName); if (sheetGenerator != null) { sheetGenerator.generate(monitor, workbook, 0, useLogicalNameAsSheetName, this.sheetNameMap, this.sheetObjectMap, diagram, loopDefinitionMap); workbook.removeSheetAt(0); } else { System.out.println("non sheet generator : " + templateSheetName); if (!isExcludeTarget(templateSheetName)) { moveSheet(workbook, 0); HSSFSheet sheet = workbook.getSheetAt(workbook.getNumberOfSheets() - 1); this.sheetObjectMap.put(templateSheetName, new StringObjectModel(templateSheetName)); this.pictureSheetGenerator.setImage(workbook, sheet); if (this.sheetIndexSheetGenerator.getTemplateSheetName() .equals(templateSheetName)) { sheetIndexSheetNo = workbook.getNumberOfSheets() - originalSheetNum; String name = this.sheetIndexSheetGenerator .getSheetName(); workbook.setSheetName(workbook.getNumberOfSheets() - 1, AbstractSheetGenerator.decideSheetName(name, sheetNameMap)); } } else { workbook.removeSheetAt(0); } monitor.worked(1); } originalSheetNum--; if (monitor.isCanceled()) { throw new InterruptedException("Cancel has been requested."); } } if (sheetIndexSheetNo != -1) { this.sheetIndexSheetGenerator.generate(monitor, workbook, sheetIndexSheetNo, useLogicalNameAsSheetName, this.sheetNameMap, this.sheetObjectMap, diagram, loopDefinitionMap); } }
Example 18
Source File: ExportToExcelManager.java From ermasterr with Apache License 2.0 | 4 votes |
private void createSheetFromTemplate(final ProgressMonitor monitor, final HSSFWorkbook workbook, final ERDiagram diagram, final boolean useLogicalNameAsSheetName) throws InterruptedException { initSheetNameMap(workbook); int originalSheetNum = workbook.getNumberOfSheets(); int sheetIndexSheetNo = -1; while (originalSheetNum > 0) { final String templateSheetName = workbook.getSheetName(0); final AbstractSheetGenerator sheetGenerator = getSheetGenerator(templateSheetName); if (sheetGenerator != null) { sheetGenerator.generate(monitor, workbook, 0, useLogicalNameAsSheetName, sheetNameMap, sheetObjectMap, diagram, loopDefinitionMap); workbook.removeSheetAt(0); } else { if (!isExcludeTarget(templateSheetName)) { moveSheet(workbook, 0); final HSSFSheet sheet = workbook.getSheetAt(workbook.getNumberOfSheets() - 1); sheetObjectMap.put(templateSheetName, new StringObjectModel(templateSheetName)); if (pictureSheetGenerator != null) { pictureSheetGenerator.setImage(workbook, sheet); } if (sheetIndexSheetGenerator.getTemplateSheetName().equals(templateSheetName)) { sheetIndexSheetNo = workbook.getNumberOfSheets() - originalSheetNum; String name = sheetIndexSheetGenerator.getSheetName(); name = AbstractSheetGenerator.decideSheetName(name, sheetNameMap); monitor.subTaskWithCounter(name); workbook.setSheetName(workbook.getNumberOfSheets() - 1, name); } else { monitor.subTaskWithCounter(sheet.getSheetName()); } } else { monitor.subTaskWithCounter("Removing template sheet"); workbook.removeSheetAt(0); } monitor.worked(1); } originalSheetNum--; } if (sheetIndexSheetNo != -1) { sheetIndexSheetGenerator.generate(monitor, workbook, sheetIndexSheetNo, useLogicalNameAsSheetName, sheetNameMap, sheetObjectMap, diagram, loopDefinitionMap); } }
Example 19
Source File: ExportToExcelManager.java From ermasterr with Apache License 2.0 | 4 votes |
private void initSheetNameMap(final HSSFWorkbook workbook) { for (int i = 0; i < workbook.getNumberOfSheets(); i++) { final String sheetName = workbook.getSheetName(i); sheetNameMap.put(sheetName.toUpperCase(), 0); } }
Example 20
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(); }