Java实现excel导入导出学习笔记1 - 实现方式

须要的技术

一、strut2框架 利用其上传下载功能
二、xml解析技术 定制导入模板
三、jquery UI 制做前台 html

四、
clipboard.pngjquery

clipboard.png
HSSF 与office03-07格式对应,版本低,兼容性好
XSSF 与xlsx格式对应apache

clipboard.png

clipboard.png

clipboard.png

excel组成的几个概念:

工做薄 excel
工做表 Sheet
行记录 row
单元格 cell框架

JXL建立excel

maven中的poi的artifactId

详见 http://poi.apache.org/overview.htmlmaven

如:spa

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.8</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.8</version>
</dependency>

POI建立excel

一、建立Excel工做簿
二、建立工做表sheet
三、建立第一行 title
四、建立一个文件
五、存盘excel

clipboard.png

HSSFWorkbook
HSSFSheet
HSSFRow
HSSFCellcode

HSSFWorkbook book = new HSSFWorkbook();
HSSFSheet sheet = book.createSheet();
String[] columns = {"id","名字","性別"};
HSSFRow headeRow = sheet.createRow(0);
for (int i = 0; i < columns.length; i++) {
HSSFCell cell = headeRow.createCell(i);
cell.setCellValue(columns[i]);
}

for (int i = 1; i < 11; i++) {
HSSFRow nextRow = sheet.createRow(i);
HSSFCell cell2 = nextRow.createCell(0);
cell2.setCellValue(i);
cell2 = nextRow.createCell(1);
cell2.setCellValue("name" + i);
cell2 = nextRow.createCell(2);
cell2.setCellValue("男");

}
FileOutputStream outputStream;
try {
outputStream = new FileOutputStream(new File(fileName));
book.write(outputStream);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

POI解析excel文件

一、建立Excel,读取文件内容
二、默认读取第一个工做表xml

clipboard.png

//建立Excel,读取文件内容
             HSSFWorkbook workbook = 
                new HSSFWorkbook(FileUtils.openInputStream(file));
            //获取第一个工做表workbook.getSheet("Sheet0");
//HSSFSheet sheet = workbook.getSheet("Sheet0");
            //读取默认第一个工做表sheet
            HSSFSheet sheet = workbook.getSheetAt(0);
            int firstRowNum = 0;
            //获取sheet中最后一行行号
            int lastRowNum = sheet.getLastRowNum();
            for (int i = firstRowNum; i <=lastRowNum; i++) {
                HSSFRow row = sheet.getRow(i);
                //获取当前行最后单元格列号
                int lastCellNum = row.getLastCellNum();
                for (int j = 0; j < lastCellNum; j++) {
                    HSSFCell cell = row.getCell(j);
                    String value = cell.getStringCellValue();
                    System.out.print(value + "  ");
                }
                System.out.println();
            }
相关文章
相关标签/搜索