java response to excel (create excel)

//调用方法是代码
				List<String> infodList = getInfoFieldList(userid, propertyId, 3);
				li.add(infodList);
				String fileName = "查询统计_" + System.currentTimeMillis() + ".xls";
				response.setHeader("Expires", "0");
				response.setHeader("Cache-Control",
						"must-revalidate, post-check=0, pre-check=0");
				response.setHeader("Pragma", "public");
				response.setContentType("application/vnd.ms-excel;charset=UTF-8");
				response.setHeader(
						"Content-Disposition",
						"attachment;filename="
								+ URLEncoder.encode(fileName, "UTF-8"));
				InspectUtil.toExcelExport(response.getOutputStream(), li);
//
正式代码
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

 public static void toExcelExport(OutputStream os, List<List<String>> li) {
        // 创建工作薄
        WritableWorkbook workbook = null;
        try {

            workbook = Workbook.createWorkbook(os);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 创建新的一页
        WritableSheet sheet = workbook.createSheet("查询统计", 0);
        WritableCellFormat titleFormat1 = new WritableCellFormat();
        try {
            titleFormat1.setVerticalAlignment(VerticalAlignment.CENTRE); // 设置居中对齐
            titleFormat1.setAlignment(Alignment.CENTRE);// 设置居中对齐(这俩哪个是上下/左右对齐也没验证过)
            titleFormat1.setBorder(jxl.format.Border.ALL,
                    jxl.format.BorderLineStyle.THIN);// 给单元格加边
            sheet.mergeCells(0, 0, 0, 1);
            sheet.mergeCells(1, 0, 1, 1);
            Label rown = new Label(0, 0, "序号", titleFormat1);
            sheet.addCell(rown);
            rown = new Label(1, 0, "单位", titleFormat1);
            sheet.addCell(rown);
            List<String> theads = li.get(li.size() - 1);
            int j = 2;
            for (int i = 0; i < theads.size(); i++) {
                sheet.mergeCells(j, 0, j + 3, 0);
                rown = new Label(j, 0, theads.get(i), titleFormat1);
                sheet.addCell(rown);
                // 写入/不完整数/完整率(%)/不正确数/正确率(%)--标题
                rown = new Label(j, 1, "不完整数", titleFormat1);
                sheet.addCell(rown);
                rown = new Label(j + 1, 1, "完整率(%)", titleFormat1);
                sheet.addCell(rown);
                rown = new Label(j + 2, 1, "不正确数", titleFormat1);
                sheet.addCell(rown);
                rown = new Label(j + 3, 1, "正确率(%)", titleFormat1);
                sheet.addCell(rown);
                j += 4;
            }
            // 写入数据
            for (int i = 0; i < li.size() - 1; i++) {
                for (int t = 0; t < li.get(i).size(); t++) {
                    rown = new Label(t, i + 2, li.get(i).get(t), titleFormat1);
                    sheet.addCell(rown);
                }
            }
            workbook.write();
            workbook.close();
            os.close();
        } catch (WriteException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Java中实现上传Excel文件,你可以使用一些常见的框架和库来简化该过程。下面是一个基本的示例代码,使用Spring Boot框架和Apache POI库来实现上传Excel文件: 1. 首先,确保在pom.xml文件中添加以下依赖: ```xml <!-- Spring Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Apache POI --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <!-- 读取和写出xlsx文件需要的依赖 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 2. 创建一个Controller类来处理上传请求: ```java import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; @RestController public class ExcelUploadController { @PostMapping("/upload") public ResponseEntity<String> uploadExcel(@RequestParam("file") MultipartFile file) { // 处理上传的Excel文件 try { // 使用Apache POI库解析Excel文件 Workbook workbook = WorkbookFactory.create(file.getInputStream()); // 对Excel文件进行处理,如读取数据或写入数据库等操作 return ResponseEntity.ok("Excel文件上传成功!"); } catch (Exception e) { e.printStackTrace(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Excel文件上传失败!"); } } } ``` 3. 运行Spring Boot应用程序,并使用HTTP POST请求上传Excel文件: ``` POST /upload Content-Type: multipart/form-data Body: - file: [选择要上传的Excel文件] ``` 这是一个简单的示例,你可以根据自己的需求进行进一步的处理和操作。请确保在代码中处理异常情况,并根据实际需求进行适当的错误处理和返回。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值