美文网首页
Java导入导出Excel

Java导入导出Excel

作者: Vincent_Jiang | 来源:发表于2017-05-30 13:45 被阅读26次

    pom.xml

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

    实例

    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    ...
    HSSFWorkbook workbook = new HSSFWorkbook(); // 工作表
    HSSFSheet sheet = workbook.createSheet();   // 表页
    HSSFRow row = sheet.createRow(0);           // 单元行
    
    row.createCell(0).setCellValue("编号");
    row.createCell(1).setCellValue("产品名称");
    row.createCell(2).setCellValue("星级");
    row.createCell(3).setCellValue("单价");
    row.createCell(4).setCellValue("图片");
    row.createCell(5).setCellValue("所属类别");
    row.createCell(6).setCellValue("类别排名");
    
    for (int i = 0; i < infos.size(); i++) {
        ProductInfo info = infos.get(i);
        row = sheet.createRow(i + 1);
        row.createCell(0).setCellValue("No." + i);
        row.createCell(1).setCellValue(info.getName());
        row.createCell(2).setCellValue(info.getStar());
        row.createCell(3).setCellValue(info.getPrice());
        row.createCell(4).setCellValue(info.getImage());
        row.createCell(5).setCellValue(info.getType());
        row.createCell(6).setCellValue(info.getRanking());
    }
    
    try {
        FileOutputStream fos = new 
        FileOutputStream("/Users/vincent/Desktop/Test.xls");
        workbook.write(fos);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    

    相关文章

      网友评论

          本文标题:Java导入导出Excel

          本文链接:https://www.haomeiwen.com/subject/jslrfxtx.html