java word操作 通过字符串生成word文件,读取word内容,word转pdf、html,pdf流输出展示到页面,file转 MultipartFile

您所在的位置:网站首页 java读取word内容转html java word操作 通过字符串生成word文件,读取word内容,word转pdf、html,pdf流输出展示到页面,file转 MultipartFile

java word操作 通过字符串生成word文件,读取word内容,word转pdf、html,pdf流输出展示到页面,file转 MultipartFile

2024-07-17 08:40:08| 来源: 网络整理| 查看: 265

 一、java 通过字符串生成word文件

POI maven配置

4.1.0 org.apache.poi poi ${poi.version} org.apache.poi poi-ooxml ${poi.version} org.apache.poi poi-ooxml-schemas ${poi.version} org.apache.poi poi-scratchpad ${poi.version} fr.opensagres.xdocreport org.apache.poi.xwpf.converter.xhtml 1.0.6

方法一: 

/** *text 要写入的文本 fileName 文件名 */ public static String createFileWord(String text,String fileName) { String savePath = null; FileOutputStream out = null; XWPFDocument document = null; FileInputStream fileInputStream = null; try { if(StringUtils.isNotBlank(id) && StringUtils.isNotBlank(text)){ String dateDir = DateUtils.formatDate(System.currentTimeMillis(), "yyyyMMdd");// 生成文件夹 savePath = dateDir;//文件夹路径 File fileDir = new File(savePath); if(!fileDir.exists()) { fileDir.mkdirs(); } savePath = savePath+fileName+".docx"; //保存的文件位置 File file = new File(savePath); if(!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } // 写入文件 out = new FileOutputStream(file); document = new XWPFDocument(); XWPFParagraph paragraph = document.createParagraph(); List list = Arrays.asList(text.split("\n")); for (String s : list) { XWPFRun run = paragraph .createRun(); run.addTab(); run.setText(s); run.addBreak(); run.setFontSize(16); } document.write(out); } } catch (Exception e) { e.printStackTrace(); }finally { try { document.close(); out.close(); fileInputStream.close(); // 删除生成的文件 FileUtils.deleteFile(savePath); } catch (IOException e) { e.printStackTrace(); } } return savePath; }

创建doc文件:

savePath = "d://"+separator+savePath+fileName+".docx"; File file = new File(savePath);

写入文本内容:

import org.apache.poi.xwpf.usermodel.XWPFRun; // 写入文件 out = new FileOutputStream(file); document = new XWPFDocument(); XWPFParagraph paragraph = document.createParagraph(); List list = Arrays.asList(text.split("\n")); for (String s : list) { XWPFRun run = paragraph .createRun(); run.addTab(); run.setText(s); //文本内容 run.addBreak(); run.setFontSize(16); } document.write(out);

方法二:

import org.apache.poi.poifs.filesystem.POIFSFileSystem; import java.io.OutputStream; //text 要写入的字符串 fileName 文件名 public static String createWord2(String text,String fileName) { try { InputStream is = new ByteArrayInputStream(text.getBytes("GBK")); String dateDir = DateUtils.formatDate(System.currentTimeMillis(), "yyyyMMdd"); String savePath = dateDir; savePath = savePath+fileName+".doc"; File dirFile = new File(savePath); OutputStream os = new FileOutputStream(savePath); POIFSFileSystem fs = new POIFSFileSystem(); fs.createDocument(is, "WordDocument"); fs.writeFilesystem(os); fs.close(); is.close(); fileInputStream.close(); } catch (Exception e) { e.printStackTrace(); } return uploadAttachment; }   二、java 读取 word内容 import org.apache.poi.xwpf.extractor.XWPFWordExtractor; import org.apache.poi.hwpf.extractor.WordExtractor; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.ooxml.POIXMLDocument; import org.apache.poi.ooxml.extractor.POIXMLTextExtractor; public static String readWord(File file) { String path = file.getPath(); String buffer = ""; try { if (path.endsWith(".doc")) { InputStream is = new FileInputStream(new File(path)); WordExtractor ex = new WordExtractor(is); buffer = ex.getText(); } else if (path.endsWith("docx")) { OPCPackage opcPackage = POIXMLDocument.openPackage(path); POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage); buffer = extractor.getText(); } else { System.out.println("此文件不是word文件!"); } } catch (Exception e) { e.printStackTrace(); } return buffer; } // public static String readWord(String path) { String buffer = ""; try { if (path.endsWith(".doc")) { InputStream is = new FileInputStream(new File(path)); WordExtractor ex = new WordExtractor(is); buffer = ex.getText(); } else if (path.endsWith("docx")) { OPCPackage opcPackage = POIXMLDocument.openPackage(path); POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage); buffer = extractor.getText(); } else { System.out.println("此文件不是word文件!"); } } catch (Exception e) { e.printStackTrace(); } return buffer; } 三、word文件转换HTML

1word转html

方法1:

//jar包非免费 19.5jdk com.aspose aspose-words ${aspose.version} com.aspose aspose-cells ${aspose.version} import com.aspose.words.HtmlSaveOptions; import java.io.ByteArrayOutputStream; public static String getToHtmlForAspose(String path) { String buffer = ""; try { if(StringUtils.isNotBlank(path)){ com.aspose.words.Document doc = new com.aspose.words.Document(path); // doc.save("C:/Users/Administrator/Desktop/广西/立法/1.html", SaveFormat.HTML); if(doc != null){ HtmlSaveOptions saveOptions = new HtmlSaveOptions(); saveOptions.setExportHeadersFootersMode(ExportHeadersFootersMode.NONE); // HtmlSaveOptions的其他设置信息请参考相关API ByteArrayOutputStream htmlStream = new ByteArrayOutputStream(); doc.save(htmlStream, saveOptions); // String htmlText = new String(htmlStream.toByteArray(),"UTF-8"); buffer = new String(htmlStream.toByteArray(),"UTF-8"); htmlStream.close(); } } } catch (Exception e) { e.printStackTrace(); } return buffer; }

方法2:

import com.aspose.words.Document; import com.aspose.words.SaveFormat; /** * doc、docx文件转PDF */ public static void wordToHtml(File sourceFile,OutputStream os) { if(null == sourceFile || !sourceFile.exists()) throw new RuntimeException("需要转换的文件不存在"); InputStream docIs = null; try { docIs = new FileInputStream(sourceFile); //输出路径 Document docum = new Document(docIs); docum.save(os, SaveFormat.HTML); }catch (Exception e){ throw new RuntimeException("word转换PDF出错"); }finally { if(docIs != null){ try { docIs.close(); } catch (IOException e) { e.printStackTrace(); } } } }

方法3:

import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions; import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.converter.WordToHtmlConverter; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.TransformerFactory; import javax.xml.transform.Transformer; // word 转 html public static String wordToHtml(String filePath){ try { if(filePath.endsWith(".doc")){ String content=convert2Html(filePath); return content; } if(filePath.endsWith(".docx")){ String content=docxToHtml(filePath); return content; } } catch (Exception e) { e.printStackTrace(); } return null; } //docx转html--------------------------------------------------- 1111 ---------------------------------------------- //生成html文件 //输出html标签和内容 public static String docxToHtml(String sourceFileName) throws Exception { String htmlPath=sourceFileName.substring(0,sourceFileName.indexOf("."))+".html"; XWPFDocument document = new XWPFDocument(new FileInputStream(sourceFileName)); XHTMLOptions options = XHTMLOptions.create().indent(4); File outFile = new File(htmlPath); outFile.getParentFile().mkdirs(); // OutputStream out = new FileOutputStream(outFile); // XHTMLConverter.getInstance().convert(document,out, options); ByteArrayOutputStream baos = new ByteArrayOutputStream(); document.createNumbering(); XHTMLConverter.getInstance().convert(document, baos, options); baos.close(); String content =new String(baos.toByteArray()); //替换UEditor无法识别的转义字符 String htmlContent1=content.replaceAll("“","\"").replaceAll("”","\"").replaceAll("—","-"); return htmlContent1; } //doc 转 html public static String convert2Html(String fileName) throws TransformerException, IOException, ParserConfigurationException { String htmlPath=fileName.substring(0,fileName.indexOf("."))+".html"; HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(fileName));//WordToHtmlUtils.loadDoc(new FileInputStream(inputFile)); //兼容2007 以上版本 WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter( DocumentBuilderFactory.newInstance().newDocumentBuilder() .newDocument()); wordToHtmlConverter.processDocument(wordDocument); //解析html Document htmlDocument = wordToHtmlConverter.getDocument(); ByteArrayOutputStream out = new ByteArrayOutputStream(); DOMSource domSource = new DOMSource(htmlDocument); StreamResult streamResult = new StreamResult(out); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty(OutputKeys.METHOD, "HTML"); serializer.transform(domSource, streamResult); out.close(); writeFile(new String(out.toByteArray()), htmlPath); //输出html String htmlContent=new String(out.toByteArray()); //替换UEditor无法识别的转义字符 String htmlContent1=htmlContent.replaceAll("“","\"").replaceAll("”","\"").replaceAll("—","-"); return htmlContent1; } public static void writeFile(String content, String path) { FileOutputStream fos = null; BufferedWriter bw = null; try { File file = new File(path); fos = new FileOutputStream(file); bw = new BufferedWriter(new OutputStreamWriter(fos,"utf-8")); bw.write(content); } catch (FileNotFoundException fnfe) { fnfe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } finally { try { if (bw != null) bw.close(); if (fos != null) fos.close(); } catch (IOException ie) { } } } 三、word转pdf import com.aspose.words.Document; /** * doc、docx文件转PDF */ public static void wordToPdf(File sourceFile,String targetPath) { //sourceFile 源文件 File targetFile = new File(targetPath+targetFileName); //输出的文件 FileOutputStream os = new FileOutputStream(targetFile); //调用 response.getOutputStream() 可输出流到页面 if(null == sourceFile || !sourceFile.exists()) throw new RuntimeException("需要转换的文件不存在"); InputStream docIs = null; try { docIs = new FileInputStream(sourceFile); //输出路径 Document docum = new Document(docIs); docum.save(os, SaveFormat.PDF); }catch (Exception e){ throw new RuntimeException("word转换PDF出错"); }finally { if(docIs != null){ try { docIs.close(); } catch (IOException e) { e.printStackTrace(); } } } }

excel to Pdf:

//sourceFile 源文件 os输出的文件 调用 response.getOutputStream() 可输出流到页面 public static void excelToPdf(File sourceFile,OutputStream os) { // File targetFile = new File(targetPath); //输出的文件 // FileOutputStream os = new FileOutputStream(targetFile); InputStream is = null; try { is = new FileInputStream(sourceFile); Workbook wb = new Workbook(is); wb.save(os, com.aspose.cells.SaveFormat.PDF); }catch (Exception e){ throw new RuntimeException("Excel转PDF错误"); }finally { if(null != is){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } }

 磁盘pdf文件流输出到页面展示

后台:

public void viewPdf(Attachment attachment,HttpServletResponse response) { FileInputStream fIn = null; OutputStream out = null; attachment = attachmentService.get(attachment); //获取附件信息用的 只看路径就行了 String path = Global.getConfig("userfiles.basedir")+"/"+attachment.getAttPath();//文件在磁盘的位置路径 try { response.addHeader("Content-Disposition", "filename=\"" + new String(path.getBytes("gb2312"), "ISO8859-1") + "\""); response.setHeader("Content-Type", "application/pdf"); File file = new File(path); if(!file.exists()){ return; } // 读取下载文件 fIn = new FileInputStream(file); // 输出流 out = response.getOutputStream(); // 缓冲区 byte[] buffer = new byte[1024]; int len = 0; while ((len = fIn.read(buffer, 0, buffer.length)) != -1) { out.write(buffer, 0, len); } out.flush(); } catch (IOException ioe) { ioe.printStackTrace(); } finally { IOUtils.closeQuietly(fIn); IOUtils.closeQuietly(out); } }

前台:

通过ifram  type=application/pdf   可直接展示后台传出的流文件

若pdf文件在项目中  可直接src=“pdf文件路径”

 

file转 MultipartFile import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.springframework.util.FileCopyUtils; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.commons.CommonsMultipartFile; import javax.activation.MimetypesFileTypeMap; import java.io.ByteArrayInputStream; import java.io.File; import java.io.InputStream; import java.io.OutputStream; public static MultipartFile toMultipartFile(String fieldName, File file) throws Exception { DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); String contentType = new MimetypesFileTypeMap().getContentType(file); //FileItem fileItem = diskFileItemFactory.createItem(fieldName, contentType, false, file.getName()); FileItem fileItem = diskFileItemFactory.createItem(fieldName, contentType, false, fieldName); try (InputStream inputStream = new ByteArrayInputStream(FileCopyUtils.copyToByteArray(file)); OutputStream outputStream = fileItem.getOutputStream()) { FileCopyUtils.copy(inputStream, outputStream); } catch (Exception e) { throw e; } MultipartFile multipartFile = new CommonsMultipartFile(fileItem); return multipartFile; } public static MultipartFile toMultipartFile(String fieldName, String fileName, byte[] fileByteArray) throws Exception { DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); String contentType = new MimetypesFileTypeMap().getContentType(fileName); FileItem fileItem = diskFileItemFactory.createItem(fieldName, contentType, false, fileName); try (InputStream inputStream = new ByteArrayInputStream(fileByteArray); OutputStream outputStream = fileItem.getOutputStream()) { FileCopyUtils.copy(inputStream, outputStream); } catch (Exception e) { throw e; } MultipartFile multipartFile = new CommonsMultipartFile(fileItem); return multipartFile; }

 



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


    图片新闻

    实验室药品柜的特性有哪些
    实验室药品柜是实验室家具的重要组成部分之一,主要
    小学科学实验中有哪些教学
    计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
    实验室各种仪器原理动图讲
    1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
    高中化学常见仪器及实验装
    1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
    微生物操作主要设备和器具
    今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
    浅谈通风柜使用基本常识
     众所周知,通风柜功能中最主要的就是排气功能。在

    专题文章

      CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭