Java 导入导出excel文档

修改老项目,做导入导出的操作比较频繁,记录一哈。。。

导入

这里做的操作是获取一个xls文档的客户编码和客户电话。

//得到所有数据
    Map<String,String> map=new HashMap<String, String>();
// 读取Excel文件
//    File file = new File("文档存放路径");
    try {
        // 创建输入流,读取Excel
        InputStream is = new FileInputStream(file.getAbsolutePath());
        // jxl提供的Workbook类
//        Workbook templateBook = new XSSFWorkbook(is);
        Workbook templateBook = new HSSFWorkbook(new POIFSFileSystem(is));
        /**
        这里要注意excel文档的版本,如果是xlsx版本的需要用XSSFWorkbook()
        xls版本的使用  HSSFWorkbook(new POIFSFileSystem(is))
           */

    Integer pagesNum = templateBook.getNumberOfSheets();    //Excel表格页数量
    Integer count = 0; //数据量

    //Excel不存在内容
    if (pagesNum == 0) {
        System.out.println("表格额无数据!!!");
        return;
    } else {
        System.out.println("共" + pagesNum + "页表格!");
    }

    Sheet sheet = templateBook.getSheetAt(0);   //获取Excel第1页

    //读取第p页每一行
    Integer sheetRowNum = sheet.getLastRowNum();
    count = count + sheetRowNum;
    System.out.println("共" + count + "行数据!");
    
			//一般来讲表格第一个行是标题,不需要读取,所以读取初始行数为1
    for (int r = 1; r <= sheetRowNum; r++) {
        //读取第p页第r行,若为空行则记录日志,继续读取下一行
        Row row = sheet.getRow(r);
        if (row == null) {
            System.out.println("第" + row + "行无数据");
            continue;
        }
        //先获取单个表格,为防止读取数据类型不兼容,强制将该表格内容类型改成字符串,获取值注意使用trim()方法去除空格
        Cell customer = row.getCell(0);
        customer.setCellType(Cell.CELL_TYPE_STRING);
        String cellValue = customer.getStringCellValue().trim();
        Cell phone = row.getCell(2);
        phone.setCellType(Cell.CELL_TYPE_STRING);
        String phoneValue = phone.getStringCellValue().trim();
        map.put(cellValue,phoneValue);
    }
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
System.out.println("共提取" + map.size() + "行数据");

导出

这里导出的是xls版本,如是xlsx版本请自行将HSSFWorkbook改为XSSFWorkbook

//开始导出
Workbook workbook = new XSSFWorkbook();
Sheet outputSheet = workbook.createSheet();    //创建表格

//设置单元格样式
outputSheet.setDefaultRowHeight((short) (2 * 256));//设置行高
outputSheet.setDefaultColumnWidth(4000);//设置列宽
XSSFFont font = (XSSFFont) workbook.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 16);

List<CellStyle> rowStyles = new ArrayList<CellStyle>();
Row titleRow = outputSheet.createRow(0);    //创建第一行
Cell cell = titleRow.createCell(0);     				//创建单元格
CellStyle style = workbook.createCellStyle();   //创建单元格的样式
cell.setCellStyle(style);
cell.setCellType(Cell.CELL_TYPE_STRING);      //设置该单元格的数据类型
cell.setCellValue("goodsId");								 //设置该单元格的标题(该单元格是头)

notExistslist是一个存放导出数据的数列

for (int j = 0; j < notExistslist.size(); j++) {
	//创建一行,再将新创的单元格加入到该行
    Row dataRow = outputSheet.createRow(1 + j);
    Cell outputCell = dataRow.createCell(0);
    outputCell.setCellStyle(style);
	
	//循环获取list的数据,并设置输入单元格的样式数据
    String templateCellValue = notExistslist.get(j);
    outputCell.setCellType(Cell.CELL_TYPE_STRING);
    outputCell.setCellValue(templateCellValue);
}

FileOutputStream fileOut = null;
try {
    fileOut = new FileOutputStream("导出的xlsx路径");
    workbook.write(fileOut);
} catch
        (IOException e
        ) {
    e.printStackTrace(

    );
} finally {
    if (fileOut != null) {
        try {
            fileOut.close(
            );
        } catch (IOException e
                ) {
            e.printStackTrace(
            );
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值