java使用POI导入导出

    1.导入相关依赖


			org.apache.poi
			poi
			3.17
		

		
			org.apache.poi
			poi-ooxml
			3.17

      2.创建测试.exce

java使用POI导入导出_第1张图片

    3.编码

public static void main(String[] args) throws IOException {

    //先从桌面读取 然后输出
    List areaList = importXLS();
    exportExcel2(areaList);


}

//从excel导入数据
public static List importXLS() throws IOException {
    //创建文件输入流
    FileInputStream is = new FileInputStream("C:\\Users\\admin\\Desktop\\测试.xls");
    //创建excel文档对象
    HSSFWorkbook workbook = new HSSFWorkbook(is);
    //获取excel工作表对象
    HSSFSheet sheetAt = workbook.getSheetAt(0);
    ArrayList list = new ArrayList<>();

    for (Row row : sheetAt) {
        //跳过标题(首行)
        if (row.getRowNum() == 0) {
            continue;
        }
        //读取第一行的数据
        String num = row.getCell(0).getStringCellValue();
        String province = row.getCell(1).getStringCellValue();
        String city = row.getCell(2).getStringCellValue();
        String district = row.getCell(3).getStringCellValue();

        Area area = new Area();
        area.setNum(num);
        area.setProvince(province);
        area.setCity(city);
        area.setDistrict(district);
        list.add(area);
    }

    System.out.println(list);
    return list;
}

//导出数据到excel
public static void exportExcel(List areaList) throws IOException {
    //1.在内存中创建同一个excel文件
    HSSFWorkbook workbook = new HSSFWorkbook();
    //2.创建工作簿
    HSSFSheet sheet = workbook.createSheet();
   /* sheet.setDisplayGridlines(false);//去除网格线*/
    //3.创建标题行 第一行需要合并 第二行正常标题
    HSSFRow firstRow = sheet.createRow(0);
    firstRow.createCell(0).setCellValue("区域信息统计");
    CellRangeAddress cellAddresses = new CellRangeAddress(0, 0, 0, 3);
    sheet.addMergedRegion(cellAddresses);
    HSSFCellStyle style = workbook.createCellStyle();
    style.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直
    style.setAlignment(HorizontalAlignment.CENTER);// 水平
    Font font = workbook.createFont();
    font.setBold(true);
    font.setFontName("宋体");
    font.setFontHeight((short) 350);
    style.setFont(font);

    HSSFRow titleRow = sheet.createRow(1);
    titleRow.createCell(0).setCellValue("序号");
    titleRow.createCell(1).setCellValue("省");
    titleRow.createCell(2).setCellValue("市");
    titleRow.createCell(3).setCellValue("区");
    //4.遍历数据,创建数据行
    for (Area area : areaList) {
        //获取最后一行的行号
        int lastRowNum = sheet.getLastRowNum();
        //总数
        HSSFRow dataRow = sheet.createRow(lastRowNum + 1);
        dataRow.createCell(0).setCellValue(area.getNum());
        dataRow.createCell(1).setCellValue(area.getProvince());
        dataRow.createCell(2).setCellValue(area.getCity());
        dataRow.createCell(3).setCellValue(area.getDistrict());
    }
    //5.创建文件名
    String fileName = "C:\\Users\\admin\\Desktop\\export.xls";
    File file = new File(fileName);
    //6.获取文件输出流
    FileOutputStream os = new FileOutputStream(file);
    workbook.write(os);
    //7.关闭流
    workbook.close();
    os.close();


}

你可能感兴趣的:(java基础,POI,java,excel导入导出)