本文整理汇总了Java中org.apache.poi.hssf.usermodel.HSSFWorkbook.getNumberOfSheets方法的典型用法代码示例。如果您正苦于以下问题:Java HSSFWorkbook.getNumberOfSheets方法的具体用法?Java HSSFWorkbook.getNumberOfSheets怎么用?Java HSSFWorkbook.getNumberOfSheets使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.poi.hssf.usermodel.HSSFWorkbook
的用法示例。
在下文中一共展示了HSSFWorkbook.getNumberOfSheets方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readXls
import org.apache.poi.hssf.usermodel.HSSFWorkbook; //导入方法依赖的package包/类
/**
* Read the Excel 2003-2007
*
* @param path
* the path of the Excel
* @return
* @throws IOException
*/
public static String readXls(String path) throws IOException {
InputStream is = new FileInputStream(path);
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
StringBuffer sb = new StringBuffer("");
// Read the Sheet
for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// Read the Row
for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow != null) {
HSSFCell no = hssfRow.getCell(0);
HSSFCell name = hssfRow.getCell(1);
sb.append(no + ":" + name);
sb.append(";");
}
}
}
return sb.toString().substring(0, sb.toString().length() - 1);
}
示例2: readExcel
import org.apache.poi.hssf.usermodel.HSSFWorkbook; //导入方法依赖的package包/类
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();
}
示例3: countSheetFromTemplate
import org.apache.poi.hssf.usermodel.HSSFWorkbook; //导入方法依赖的package包/类
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;
}
示例4: countSheetFromTemplate
import org.apache.poi.hssf.usermodel.HSSFWorkbook; //导入方法依赖的package包/类
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++;
}
}
if (this.exportExcelSetting.isPutERDiagramOnExcel()) {
count += 1;
}
return count;
}
示例5: working
import org.apache.poi.hssf.usermodel.HSSFWorkbook; //导入方法依赖的package包/类
private void working(InputStream stream) throws Exception {
HSSFWorkbook workbook = new HSSFWorkbook(stream);
int sheetNumber = workbook.getNumberOfSheets();
for (int i = 0; i < sheetNumber; i++) {
createTable(workbook.getSheetAt(i));
}
database.close();
}
示例6: xlsToHtml
import org.apache.poi.hssf.usermodel.HSSFWorkbook; //导入方法依赖的package包/类
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();
}
示例7: initSheetNameMap
import org.apache.poi.hssf.usermodel.HSSFWorkbook; //导入方法依赖的package包/类
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);
}
}
示例8: createSheetFromTemplate
import org.apache.poi.hssf.usermodel.HSSFWorkbook; //导入方法依赖的package包/类
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);
}
}
示例9: initSheetNameMap
import org.apache.poi.hssf.usermodel.HSSFWorkbook; //导入方法依赖的package包/类
private void initSheetNameMap(HSSFWorkbook workbook) {
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
String sheetName = workbook.getSheetName(i);
this.sheetNameMap.put(sheetName.toUpperCase(), 0);
}
}
示例10: createSheetFromTemplate
import org.apache.poi.hssf.usermodel.HSSFWorkbook; //导入方法依赖的package包/类
private void createSheetFromTemplate(ProgressMonitor monitor,
HSSFWorkbook workbook, ERDiagram diagram,
boolean useLogicalNameAsSheetName) throws InterruptedException {
this.initSheetNameMap(workbook);
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 {
if (!isExcludeTarget(templateSheetName)) {
moveSheet(workbook, 0);
HSSFSheet sheet = workbook.getSheetAt(workbook
.getNumberOfSheets() - 1);
this.sheetObjectMap.put(templateSheetName,
new StringObjectModel(templateSheetName));
if (this.pictureSheetGenerator != null) {
this.pictureSheetGenerator.setImage(workbook, sheet);
}
if (this.sheetIndexSheetGenerator.getTemplateSheetName()
.equals(templateSheetName)) {
sheetIndexSheetNo = workbook.getNumberOfSheets()
- originalSheetNum;
String name = this.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) {
this.sheetIndexSheetGenerator.generate(monitor, workbook,
sheetIndexSheetNo, useLogicalNameAsSheetName,
this.sheetNameMap, this.sheetObjectMap, diagram,
loopDefinitionMap);
}
}
示例11: readExcel2003
import org.apache.poi.hssf.usermodel.HSSFWorkbook; //导入方法依赖的package包/类
public static ArrayList<ArrayList<ArrayList<Object>>> readExcel2003(File file){
try{
ArrayList<ArrayList<ArrayList<Object>>> sheetArray = new ArrayList<ArrayList<ArrayList<Object>>> ();
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(file));
for(int sheetNum = 0;sheetNum < wb.getNumberOfSheets();sheetNum++){
ArrayList<ArrayList<Object>> rowList = new ArrayList<ArrayList<Object>>();
ArrayList<Object> colList;
HSSFSheet sheet = wb.getSheetAt(sheetNum);
HSSFRow row;
HSSFCell cell;
Object value;
for(int i = 0 , rowCount = 0; rowCount < sheet.getPhysicalNumberOfRows() ; i++ ){
row = sheet.getRow(i);
colList = new ArrayList<Object>();
if(row == null){
//����ȡ��Ϊ��ʱ
if(i != sheet.getPhysicalNumberOfRows()){//�ж��Ƿ������һ��
rowList.add(colList);
}
continue;
}else{
rowCount++;
}
for( int j = 0 ; j <= row.getLastCellNum() ;j++){
cell = row.getCell(j);
if(cell == null ){
//���õ�Ԫ��Ϊ��
if(j != row.getLastCellNum()){//�ж��Ƿ��Ǹ��������һ����Ԫ��
colList.add("");
}
continue;
}
switch(cell.getCellType()){
case XSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case XSSFCell.CELL_TYPE_NUMERIC:
if ("@".equals(cell.getCellStyle().getDataFormatString())) {
value = df.format(cell.getNumericCellValue());
} else if ("General".equals(cell.getCellStyle()
.getDataFormatString())) {
value = nf.format(cell.getNumericCellValue());
} else {
value = sdf.format(HSSFDateUtil.getJavaDate(cell
.getNumericCellValue()));
}
break;
case XSSFCell.CELL_TYPE_BOOLEAN:
value = Boolean.valueOf(cell.getBooleanCellValue());
break;
case XSSFCell.CELL_TYPE_BLANK:
value = "";
break;
default:
value = cell.toString();
}// end switch
colList.add(value);
}//end for j
rowList.add(colList);
}//end for i
sheetArray.add(rowList);
}// end sheetNum
return sheetArray;
}catch(Exception e){
return null;
}
}