Java后台创建word文档
Java后台创建word⽂档
---恢复内容开始---
word打开是乱码
                          Java 后台创建 word ⽂档
  ⾃⼰总结⽹上查阅的⽂档
  ⽅式⼀、(正常创建适合短⽂本⽆格式要求Word⽂档)
1    XWPFDocument doc = new XWPFDocument(); //创建word⽂件
2    XWPFParagraph p1 = ateParagraph(); //创建段落
3    XWPFRun r1 = p1.createRun(); //创建段落⽂本
4    r1.setText("hello world"); //设置⽂本
5    FileOutputStream out = new FileOutputStream("D:\\simple.docx"); //创建输出流
6    doc.write(out);  //输出2022年几点几分立秋
7    out.close();  //关闭输出流
  ⽅式⼆、(适合有格式要求的Word⽂档。)
Demo code is my Working code  这段code  是⽂件下载功能对选中的项进⾏数据读取并且⽣成word⽂档并且压缩成zip格式下载//1、创建临时⽂件夹是否存在
File zipFolder = new File("F:\\压缩word");
if(!ists()){
zipFolder.mkdirs();
}
File wordFolder = new File("F:\\测试word");
if(!ists()){
wordFolder.mkdirs();
}
//2、声明变量
String splitSymbol = "\\";
byte[] byt = new byte[1024];
苏格兰折耳猫价格
int count = 0;
//编写压缩后⽂件流
FileOutputStream zipOut = new FileOutputStream(zipFolder+splitSymbol+"myfigs.zip");
//获得 zip的file对象
File fileZip = new File(zipFolder+splitSymbol+"myfigs.zip");
//3、判断⽤户选择是1个还是多个
//获得类型
String[] libList = Parameter("libSource").split(",");
/
/迭代ID
String[] idArray = Parameter("hidFavId").split(",");
for(int i = 0;i<idArray.length;i++){
啥叫白领//4、根据收藏类型区分访问数据库获得word正⽂内容数据
String wordText = getTextData(request,idArray[i],libList[i]);
//3、新创建个word
StringBuffer buffer = new StringBuffer(); //因为必须符合HTML格式所以必须添加html头
buffer.append("<html><head><body>");
buffer.append(wordText);
buffer.append("</body></head></html>");
//⽣成个⽂件名
UUID uuid = UUID.randomUUID();
File outFile = new File(wordFolder+String()+".doc");
//将数据转成字节数组
byte[] contentBytes = String().getBytes();
ByteArrayInputStream byteStream = new ByteArrayInputStream(contentBytes);
//创建word⽂档
POIFSFileSystem poifSystem = new POIFSFileSystem(); //暂时不知道什么意思
DirectoryNode root = Root(); //同上
FileOutputStream outStream = new FileOutputStream(outFile);
poifSystem.writeFilesystem(outStream); //将流输出到word⽂档上
byteStream.close();
outStream.close();
里约奥运会开幕
}
//5、利⽤压缩流对进⾏压缩
ZipOutputStream zipOutStream = new ZipOutputStream(new BufferedOutputStream(zipOut)); //创建个压缩流对应⽂件输出流⽂件名及路径// 获得要压缩的⽂件夹
File file = new File(wordFolder+splitSymbol);
String[] fileList = file.list();
for(String fileName : fileList){ //迭代要压缩⽂件夹⼦内容
FileInputStream inputStream = new FileInputStream(file+splitSymbol+fileName);
//设置压缩⽂件打开后显⽰每⼀项的名字
ZipEntry zipentry = new ZipEntry(fileName);
zipOutStream.putNextEntry(zipentry); //将压缩流和⾃项关联上
while((ad(byt, 0, 1024))!=-1){
zipOutStream.write(byt, 0, count);
}
inputStream.close();
}
zipOutStream.close();
//7、⽂件下载
String fileName = new String("aggregate.zip".getBytes("UTF-8"),"iso-8859-1");
response.setHeader("Content-Disposition", "attachment; filename="+fileName); //⽂件名
response.setContentType("application/octet-stream; charset=utf-8");
response.setHeader("Content-Length", Long.valueOf(fileZip.length()).toString()); //页⾯下载框⽂件后⾯()⼤⼩        response.setCharacterEncoding("UTF-8");
ServletOutputStream outputStream = OutputStream();
byte[] byteArray = adFileToByteArray(fileZip); //获得要下载的⽂件字节数组
//8、下载之前将临时⽬录删除
File[] zipFiles = zipFolder.listFiles();
File[] wordFiles = wordFolder.listFiles();
for(File zip : zipFiles){
zip.delete();
}
for(File word : wordFiles){
word.delete();
}
zipFolder.delete();
wordFolder.delete();
outputStream.write(byteArray); //利⽤输出流输出
outputStream.flush();
---恢复内容结束---
>此物最相思的上一句是什么

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。