本文整理匯總了Java中org.apache.poi.xssf.usermodel.XSSFWorkbook.getNumberOfSheets方法的典型用法代碼示例。如果您正苦於以下問題:Java XSSFWorkbook.getNumberOfSheets方法的具體用法?Java XSSFWorkbook.getNumberOfSheets怎麽用?Java XSSFWorkbook.getNumberOfSheets使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.poi.xssf.usermodel.XSSFWorkbook
的用法示例。
在下文中一共展示了XSSFWorkbook.getNumberOfSheets方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: parseExcel
import org.apache.poi.xssf.usermodel.XSSFWorkbook; //導入方法依賴的package包/類
@SuppressWarnings("resource")
private List<Map<String,Object>> parseExcel(InputStream stream) throws Exception {
List<Map<String,Object>> mapList=new ArrayList<Map<String,Object>>();
XSSFWorkbook wb = new XSSFWorkbook(stream);
for (int i = 0; i < wb.getNumberOfSheets(); i++) {
XSSFSheet sheet = wb.getSheetAt(i);
if (sheet == null) {
continue;
}
String name = sheet.getSheetName();
Map<String,Object> map=new HashMap<String,Object>();
map.put("name",name);
map.put("data", buildVariables(sheet));
mapList.add(map);
}
return mapList;
}
示例2: parse
import org.apache.poi.xssf.usermodel.XSSFWorkbook; //導入方法依賴的package包/類
/**
* Parses an inputstream containin xlsx into an outputStream containing XML
*
* @param inputStream
* the source
* @param outputStream
* the result
* @throws IOException
* @throws XMLStreamException
*/
public void parse(final InputStream inputStream, final OutputStream outputStream)
throws IOException, XMLStreamException {
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
XMLStreamWriter out = this.getXMLWriter(outputStream);
out.writeStartDocument();
out.writeStartElement("workbook");
int sheetCount = workbook.getNumberOfSheets();
for (int i = 0; i < sheetCount; i++) {
final XSSFSheet sheet = workbook.getSheetAt(i);
try {
this.export(sheet, out);
} catch (UnsupportedEncodingException | FileNotFoundException | XMLStreamException
| FactoryConfigurationError e) {
e.printStackTrace();
}
}
out.writeEndElement();
out.writeEndDocument();
out.close();
workbook.close();
}
示例3: parseFile
import org.apache.poi.xssf.usermodel.XSSFWorkbook; //導入方法依賴的package包/類
public Map<Class<? extends Inventory>, List<Inventory>> parseFile() throws Exception {
sheetFormats = formatLoader.loadFromJson();
workbook = new XSSFWorkbook(new FileInputStream(excelFile));
// greater workbook sheets doesnot matter rather if we only want to parse first
// few sheets ?
if (workbook.getNumberOfSheets() < sheetFormats.size())
throw new FileParsingException("format number of sheets is greater than excel number of sheets");
List<SheetParser> sheetParsingTasks = new ArrayList<>();
// package in which POJO resides
Class<? extends Inventory> clazz;
for (final SheetFormat sf : sheetFormats) {
clazz = InventoryFactory.getClazz(sf.getName());
if (clazz == null)
throw new SheetParsingException("Name of the Sheet doesnot match POJO Class Name");
sheetParsingTasks.add(new SheetParser(workbook.getSheetAt(sf.getIndex()), sf, clazz));
}
ExecutorService pool = Executors.newFixedThreadPool(sheetParsingTasks.size() + 2);
List<Future<List<Inventory>>> results = pool.invokeAll(sheetParsingTasks);
Map<Class<? extends Inventory>, List<Inventory>> map = new HashMap<>();
int n = results.size();
for (int count = 0, i = 0; count < n;) {
if (results.get(i) != null && results.get(i).isDone()) {
count++;
try {
List<Inventory> list = results.get(i).get();
try {
clazz = list.get(0).getClass();
// allow duplicates
if (map.containsKey(clazz))
map.get(clazz).addAll(list);
else
map.put(clazz, list);
results.set(i, null);
} catch (IndexOutOfBoundsException e) {
log.warn("Sheet " + sheetFormats.get(i).getIndex() + 1 + "/"
+ sheetFormats.get(i).getName() + " is empty");
}
} catch (ExecutionException | InterruptedException | NullPointerException ex ) {
List<String> errors = new ArrayList<String>();
Arrays.stream(ex.getStackTrace()).forEach(e->errors.add(e.toString()));
log.error("error in parsing excel file at : {}" , errors);
}
} else
i = (i + 1) % n;
}
return map;
}