java word操作 通过字符串生成word文件,读取word内容,word转pdf、html,pdf流输出展示到页面,file转 MultipartFile
程序员文章站
2024-03-17 15:04:58
...
一、java 通过字符串生成word文件
POI maven配置
<poi.version>4.1.0</poi.version>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
<version>1.0.6</version>
</dependency>
方法一:
/**
*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<String> 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<String> 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包非免费
<aspose.version>19.5jdk</aspose.version>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>${aspose.version}</version>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>${aspose.version}</version>
</dependency>
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文件路径”
<iframe src="${cfx}/flfgOut/viewPdf1?id=${attachmentPdf}" type="application/pdf" width="100%" height="99%"></iframe>
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;
}
上一篇: 牛客网 查找(二分查找)
下一篇: 二分算法学习笔记