java 导出时指定目录_java中使用POI导出excel文档到指定目录中

String fileName = "导出Excel.xlsx"; //创建名称

String rpath= null;

ClassLoader cldr=getClass().getClassLoader();if(null!=cldr){

rpath= cldr.getResource("/").getPath();

}else{

rpath="###";

}

String os= System.getProperty("os.name");//获取当前操作系统

if (os != null && os.startsWith("Windows")) {if (rpath.startsWith("/")) {

rpath= rpath.substring(1, rpath.length());

}

}if (rpath.contains("WEB-INF")) {

rpath= rpath.substring(0, rpath.indexOf("WEB-INF")) + "excel";

}

String rpathfinal= rpath +"/"+fileName;//路径

SXSSFWorkbook wb= new SXSSFWorkbook(1024); //这里1024是在内存中的数量,如果大于此数量时,会写到硬盘,以避免在内存导致内存溢出

Sheet sh =wb.createSheet();for (int i = 0; i < List.size()+1; i++) {

sh.setColumnWidth(i,4500);

Row row=sh.createRow(i);if (i==0) {

row.createCell(0).setCellValue("xxxx");

row.createCell(1).setCellValue("aaaa");

row.createCell(2).setCellValue("bbbb");

row.createCell(3).setCellValue("cccc");

}else{

row.createCell(0).setCellValue(StringUtil.isNullOrEmpty(List.get(i-1).get("belong")));

row.createCell(1).setCellValue(StringUtil.isNullOrEmpty(List.get(i-1).get("dqbj")));

row.createCell(2).setCellValue(StringUtil.isNullOrEmpty(List.get(i-1).get("datasource")));

row.createCell(3).setCellValue(StringUtil.isNullOrEmpty(List.get(i-1).get("tablename")));

}

}

FileOutputStream output=newFileOutputStream(rpathfinal);

wb.write(output);

output.close();

try {

//发送邮件给指定人

String EmailMessage = "这是您需要的excel文档,截止时间为:"+ today;

SendMailUtil.createFileMail(Email, "标题", rpathfinal, EmailMessage);

} catch (Exception e) {

e.printStackTrace();

}

//删除存在文件夹中的文档

LOGGER.info("

boolean deleteFile = DeleteFile(rpathfinal);//删除文件

LOGGER.info("

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java使用POI导出指定位置图片到Excel的示例代码: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.*; public class ImageToExcel { public static void main(String[] args) { // 创建Excel文档 Workbook workbook = new XSSFWorkbook(); // 创建Sheet Sheet sheet = workbook.createSheet(); // 设置单元格宽度 sheet.setColumnWidth(0, 5000); // 读取图片 BufferedImage image = null; try { image = ImageIO.read(new File("image.jpg")); } catch (IOException e) { e.printStackTrace(); } // 创建字节数组输出流 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try { // 将图片写入字节数组输出流 ImageIO.write(image, "jpg", byteArrayOutputStream); } catch (IOException e) { e.printStackTrace(); } // 将字节数组输出流转换为字节数组 byte[] bytes = byteArrayOutputStream.toByteArray(); // 创建图片单元格 CreationHelper helper = workbook.getCreationHelper(); Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = helper.createClientAnchor(); anchor.setCol1(0); anchor.setRow1(0); Picture picture = drawing.createPicture(anchor, workbook.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG)); // 保存Excel文档 try { FileOutputStream fileOutputStream = new FileOutputStream("image.xlsx"); workbook.write(fileOutputStream); fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 注意,以上代码的图片文件路径为相对路径,需要根据实际情况修改。此外,如果要导出其他类型的图片,如PNG、GIF等,只需要将代码的"jpg"改为相应的图片类型即可。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值