使用apache的POI实现excel下载(.xls和.xlsx)

  1. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");  
  2. response.addHeader("Content-Disposition""attachment;filename=fileName" +fileName+ ".xlsx"); 
  3. 实现.xlsx文件      从web端下载(XXHFWorkbook)

  1.  response.setContentType("application/vnd.ms-excel");  
  2.   response.addHeader("Content-Disposition""attachment;filename=fileName"+fileName+".xls");  
  3. 实现.xls文件     从web端下载Workbook workbook = new HSSFWorkbook();  

Cokie cokie =new Cokie("filename","true");



<dependency>

        <groupId>org.apache.poi<groupId>

        <artifactId>poi</artifactId>

        <version>3.14</version>

</dependency>

<dependency>

        <groupId>org.apache.poi<groupId>

        <artifactId>poi-ooxml</artifactId>

        <version>3.14</version>

</dependency>

可以实现excel上传和下载以及样式修改,关键方法

  1. XSSFWorkbook workbook =new XSSFWorkbook();
  2. XSSFSheet sheet = workbook .createSheet();//页面创建方法
  3. XSSFCellStyle style = workbook.createCellStyle();//样式创建的方法
  4. XSSFRow row= sheet.createRow(index);//第index+1行的创建方法
  5. XSSFCell cell = sheet.createCell(index);//第index+1列的创建方法;
  6. CellRangAddress car = new CellRangAddress(a,b,c,d)//ab是行位a开始位,b结束位,cd是列位c开始位,d结束位。
  7. 如果row=sheet.createRow(0)   ,createCell(0).setCellValue("你好"),第一行第一列单元格设置内容为”你好“;



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Apache POI库来解析Excel文件。下面是一个使用Java解析Excel文件的示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class ExcelParser { public static void main(String[] args) { String filePath = "path/to/your/excel/file.xlsx"; // 替换为你的Excel文件路径 try { FileInputStream fis = new FileInputStream(new File(filePath)); Workbook workbook; if (filePath.endsWith(".xlsx")) { workbook = new XSSFWorkbook(fis); // 处理.xlsx文件 } else if (filePath.endsWith(".xls")) { workbook = new HSSFWorkbook(fis); // 处理.xls文件 } else { throw new IllegalArgumentException("The specified file is not Excel file"); } Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表 for (Row row : sheet) { for (Cell cell : row) { CellType cellType = cell.getCellType(); if (cellType == CellType.STRING) { System.out.print(cell.getStringCellValue() + " "); } else if (cellType == CellType.NUMERIC) { System.out.print(cell.getNumericCellValue() + " "); } else if (cellType == CellType.BOOLEAN) { System.out.print(cell.getBooleanCellValue() + " "); } } System.out.println(); } workbook.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 请将代码中的`"path/to/your/excel/file.xlsx"`替换为你实际的Excel文件路径。该代码会打开Excel文件并输出每个单元格的值。你可以根据需要对解析的内容进行进一步处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值