Java将数据导出到Excel

public String toExcel(List<ResPreExamVO> list) throws Exception{
        // 第一步,创建一个webbook,对应一个Excel文件  
        HSSFWorkbook wb = new HSSFWorkbook();  
        // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet  
        HSSFSheet sheet = wb.createSheet("试题导出表");  
        // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short  
        HSSFRow row = sheet.createRow((int) 0);  
        // 第四步,创建单元格,并设置值表头 设置表头居中  
        HSSFCellStyle style = wb.createCellStyle();  
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
        HSSFCell cell = row.createCell((short) 0);  
        cell.setCellValue("编号");  
        cell.setCellStyle(style);  
        cell = row.createCell((short) 1);  
        cell.setCellValue("试题");  
        cell.setCellStyle(style);  
        cell = row.createCell((short) 2);  
        cell.setCellValue("选项");  
        cell.setCellStyle(style);  
        cell = row.createCell((short) 3);  
        cell.setCellValue("正确答案");  
        cell.setCellStyle(style);  
        cell = row.createCell((short) 4);  
        cell.setCellValue("试题解析");  
        cell.setCellStyle(style);
        cell = row.createCell((short) 5);  
        cell.setCellValue("试题难度");  
        cell.setCellStyle(style);
        cell = row.createCell((short) 6);  
        cell.setCellValue("关联知识点");  
        cell.setCellStyle(style);
        cell = row.createCell((short) 7);  
        cell.setCellValue("所属学段");  
        cell.setCellStyle(style);
        cell = row.createCell((short) 8);  
        cell.setCellValue("所属学科");  
        cell.setCellStyle(style);
        // 第五步,写入实体数据 实际应用中这些数据从数据库得到,  
        for (int i = 0; i < list.size(); i++) {  
            row = sheet.createRow((int) i + 1);  
            ResPreExamVO exam = list.get(i);  
            // 第四步,创建单元格,并设置值  
            if(exam.getId()!=null){
                row.createCell((short) 0).setCellValue(exam.getId());  
            }else{
                row.createCell((short) 0).setCellValue(0);  
            }
            if(exam.getExamTitle()!=null&&exam.getExamTitle()!=""){
                row.createCell((short) 1).setCellValue(exam.getExamTitle());  
            }else{
                row.createCell((short) 1).setCellValue("暂无");  
            }
            if(exam.getExamOption()!=null&&exam.getExamOption()!=""){
                row.createCell((short) 2).setCellValue(exam.getExamOption());  
            }else{
                row.createCell((short) 2).setCellValue("暂无");  
            }
            if(exam.getExamKey()!=null&&exam.getExamKey()!=""){
                row.createCell((short) 3).setCellValue(exam.getExamKey());  
            }else{
                row.createCell((short) 3).setCellValue("暂无");  
            }
            if(exam.getExamAnalysis()!=null&&exam.getExamAnalysis()!=""){
                row.createCell((short) 4).setCellValue(exam.getExamAnalysis());  
            }else{
                row.createCell((short) 4).setCellValue("暂无");  
            }
            if(exam.getExamLevel()!=null){
                row.createCell((short) 5).setCellValue(exam.getExamLevel());  
            }else{
                row.createCell((short) 5).setCellValue("暂无");  
            }
            if(exam.getPointName()!=null&&exam.getPointName()!=""){
                row.createCell((short) 6).setCellValue(exam.getPointName());  
            }else{
                row.createCell((short) 6).setCellValue("暂无");  
            }
            if(exam.getLevelName()!=null&&exam.getLevelName()!=""){
                row.createCell((short) 7).setCellValue(exam.getLevelName());  
            }else{
                row.createCell((short) 7).setCellValue("暂无");  
            }
            if(exam.getSubjectName()!=null&&exam.getSubjectName()!=""){
                row.createCell((short) 8).setCellValue(exam.getSubjectName());  
            }else{
                row.createCell((short) 8).setCellValue("暂无");  
            }
        }  
        // 第六步,将文件存到指定位置  
        try {  
            String name = "试题导出表";
            String url = "C:/Users/Administrator/Desktop/"+name+".xls";
            while (new File(url).exists()) {
                name+="-副本";
                url = "C:/Users/Administrator/Desktop/"+name+".xls";
            }
            FileOutputStream out = new FileOutputStream(url);  
            wb.write(out);  
            out.close();  
            return "T";
        }  
        catch (Exception e) {  
            e.printStackTrace();
            return "E";
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
有很多方法可以将Java中的数据导出Excel,以下是其中一种常见的方法: 1. 首先需要添加Apache POI库的依赖,在pom.xml文件中添加以下代码: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 2. 创建一个Workbook对象,代表整个Excel文件: ```java Workbook workbook = new XSSFWorkbook(); ``` 3. 创建一个Sheet对象,代表一个工作表: ```java Sheet sheet = workbook.createSheet("Sheet1"); ``` 4. 创建一行,代表Excel中的一行数据: ```java Row row = sheet.createRow(0); ``` 5. 创建单元格,代表Excel中的一个单元格: ```java Cell cell = row.createCell(0); ``` 6. 设置单元格的值: ```java cell.setCellValue("Hello World"); ``` 7. 最后通过输出流将数据写入到Excel文件中: ```java FileOutputStream fos = new FileOutputStream("output.xlsx"); workbook.write(fos); fos.close(); ``` 完整示例代码如下: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileOutputStream; import java.io.IOException; public class ExcelWriter { public static void main(String[] args) throws IOException { // 创建一个Workbook对象,代表整个Excel文件 Workbook workbook = new XSSFWorkbook(); // 创建一个Sheet对象,代表一个工作表 Sheet sheet = workbook.createSheet("Sheet1"); // 创建一行,代表Excel中的一行数据 Row row = sheet.createRow(0); // 创建单元格,代表Excel中的一个单元格 Cell cell = row.createCell(0); // 设置单元格的值 cell.setCellValue("Hello World"); // 通过输出流将数据写入到Excel文件中 FileOutputStream fos = new FileOutputStream("output.xlsx"); workbook.write(fos); fos.close(); System.out.println("Excel文件写入完成!"); } } ``` 运行以上代码后,会在项目根目录下生成一个名为output.xlsx的Excel文件,其中第一个单元格的值为Hello World。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值