2

需要的技术

1、strut2框架 利用其上传下载功能
2、xml解析技术 定制导入模板
3、jquery UI 制作前台

4、
clipboard.png

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

clipboard.png

clipboard.png

clipboard.png

excel组成的几个概念:

工作薄 excel
工作表 Sheet
行记录 row
单元格 cell

JXL创建excel

maven中的poi的artifactId

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

如:

<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

1、创建Excel工作簿
2、创建工作表sheet
3、创建第一行 title
4、创建一个文件
5、存盘

clipboard.png

HSSFWorkbook
HSSFSheet
HSSFRow
HSSFCell

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文件

1、创建Excel,读取文件内容
2、默认读取第一个工作表

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();
            }

Ocean
1.6k 声望74 粉丝

Mobaxterm