乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > java poi <em>Excel</em>2007里的SXSSFWorkb...

java poi <em>Excel</em>2007里的SXSSFWorkb...

作者:乔山办公网日期:

返回目录:excel表格制作


public SXSSFWorkbook(XSSFWorkbook workbook)

  • Construct a workbook from a template.

    There are three use-cases to use
    SXSSFWorkbook(XSSFWorkbook) :

  • Append new sheets to existing workbooks. You can open existing workbook from
    a file or create on the fly with XSSF.

  • Append rows to existing sheets. The row number MUST be greater than
    max(rownum) in the template sheet.

  • Use existing workbook as a template and re-use global objects such as cell
    styles, formats, images, etc.

  • All three use cases can work in a
    combination. What is not supported:

  • Access initial cells and rows in the template. After constructing SXSSFWorkbook(XSSFWorkbook) all internal windows are empty and SXSSFSheet.getRow(int) and SXSSFRow.getCell(int) return null.

  • Override existing cells and rows. The API silently allows that but the
    output file is invalid and Excel cannot read it.

API文档里面有,反正就是说,使用SXSSFWorkbook附加数据到已经存在的Excel中的话就是不行的;SXSSFSheet.getRow(int) 和 SXSSFRow.getCell(int) 会返回空。

SXSSFWorkbook只能用在新创建e799bee5baa6e59b9ee7ad94333Excel中才行。



Notes是e68a84e8a2ad7a686964616f362一个具有Id , RuleID , MainId 属性的javaBean.

public static List<Notes> readFromXLS2007(String filePath) {
File excelFile = null;// Excel文件对象
InputStream is = null;// 输入流对象
String cellStr = null;// 单元格,最终按字符串处理
List<Notes> NotesList = new ArrayList<Notes>();// 返回封装数据的List
Notes Notes = null;// 每一个Notes对象
try {
excelFile = new File(filePath);
is = new FileInputStream(excelFile);// 获取文件输入流
XSSFWorkbook workbook2007 = new XSSFWorkbook(is);// 创建Excel2007文件对象
XSSFSheet sheet = workbook2007.getSheetAt(0);// 取出第一个工作表,索引是0
// 开始循环遍历行,表头不处理,从1开始
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
Notes = new Notes();// 实例化Notes对象
XSSFRow row = sheet.getRow(i);// 获取行对象
if (row == null) {// 如果为空,不处理
continue;
}
// 循环遍历单元格
for (int j = 0; j < row.getLastCellNum(); j++) {
XSSFCell cell = row.getCell(j);// 获取单元格对象
if (cell == null) {// 单元格为空设置cellStr为空串
cellStr = "";
} else if (cell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {// 对布尔值的处理
cellStr = String.valueOf(cell.getBooleanCellValue());
} else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {// 对数字值的处理
cellStr = cell.getNumericCellValue() + "";
} else {// 其余按照字符串处理
cellStr = cell.getStringCellValue();
}
// 下面按照数据出现位置封装到bean中
if (j == 0) {
Notes.setId(new Double(cellStr).intValue());
} else if (j == 1) {
Notes.setRuleId(new Double(cellStr).intValue());
} else if (j == 2) {
Notes.setMainId(new Double(cellStr).intValue());
}
}
NotesList.add(Notes);// 数据装入List
}
} catch (IOException e) {
e.printStackTrace();
} finally {// 关闭文件流
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return NotesList;
}
}
①相同工作簿不同工作表间相互引用,在引用单元格前加Sheetn!(Sheetn为被引用工作表的名称)
举例:如工作表Sheet1中的A1单元格内容等于Sheet2中单元格B2乘以5,则在Sheet1中的A1单元格中输入公式 =Sheet1!B2*5
②不同工作簿间互相引用,在引用单元格前加[Book.xlsx]Sheet!(Book为被引用工作薄名,Sheet为被引用工作表名)
举例:如工作薄”Book1“中Sheet1中的A1单元格内容等于工作薄”Book2“Sheet1中单元格B2乘以5,则在Sheet1中的A1单元格中输入公式 =[Book2.xlsx]Sheet1!$B$2*5

String exportFileName = “copyd:\asd.xlsx”

InputStream fs=new FileInputStream(exportFileName); //获取存在的zdexcel文件
XSSFWorkbook xs=new XSSFWorkbook(fs);

SXSSFWorkbook wb = new SXSSFWorkbook(xs);

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网