SH *_*H A 3 arrays apache-poi xwpf
是否可以将 a 转换XWPFDocument为byte[]?我不想将其保存到文件中,因为我不需要它。如果有可能的方法可以做到这一点,那会有所帮助
XWPFDocument扩展了POIXMLDocument ,它的write方法采用 java.io.OutputStream 作为参数。那也可以是一个ByteArrayOutputStream. 因此,如果需要获取 aXWPFDocument作为字节数组,则将其写入 a 中ByteArrayOutputStream,然后从方法ByteArrayOutputStream.toByteArray获取该数组。
例子:
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
public class CreateXWPFDocumentAsByteArray {
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run=paragraph.createRun();
run.setBold(true);
run.setFontSize(22);
run.setText("The paragraph content ...");
paragraph = document.createParagraph();
ByteArrayOutputStream out = new ByteArrayOutputStream();
document.write(out);
out.close();
document.close();
byte[] xwpfDocumentBytes = out.toByteArray();
// do something with the byte array
System.out.println(xwpfDocumentBytes);
// to prove that the byte array really contains the XWPFDocument
try (FileOutputStream stream = new FileOutputStream("./XWPFDocument.docx")) {
stream.write(xwpfDocumentBytes);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4518 次 |
| 最近记录: |