「java表格排版」java自动排版

博主:adminadmin 2022-11-22 02:30:06 41

本篇文章给大家谈谈java表格排版,以及java自动排版对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java中自动排版的快捷键是什么

Eclipse自动排版快捷键

全部代码排版:Ctrl+Shift+F,选定代码排版:ctrl+I。

java word中表格如何排版输出

首先我用的技术是 poi

这是代码,一个工具类得调用

public class WordUtil {

/**

* 基于模板文件导出 word 文档,此方法主要是用来处理文档中需要替换的文本内容,对图片和表格无效

*

* @param templatePath

* 模板文件的路径,要求路径中要包含全名,并且模板文件只能是 07 及以上格式,即 docx 的文件

* @param destFilePath

* 导出文件的存放路径,包含文件名,例如,E:/test/小区公告.docx

* @param data

* 用来替换文档中预定义的字符串,要求预定义的字符串与 data 中的 key 值要相同

*/

public static void exportWordByTemplate(String templatePath,

String destFilePath, MapString, String data) {

FileOutputStream out = null;

XWPFDocument doc = null;

try {

doc = new XWPFDocument(POIXMLDocument.openPackage(templatePath));

ListXWPFRun listRun;

ListXWPFParagraph listParagraphs = doc.getParagraphs();

for (int i = 0; i listParagraphs.size(); i++) {

listRun = listParagraphs.get(i).getRuns();

for (int j = 0; j listRun.size(); j++) {

if (data.get(listRun.get(j).getText(0)) != null) {

String val = data.get(listRun.get(j).getText(0));

listRun.get(j).setText(val, 0);

}

}

}

File destFile = new File(destFilePath);

if (!destFile.getParentFile().exists()) {

destFile.getParentFile().mkdirs();

}

out = new FileOutputStream(destFilePath);

doc.write(out);

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (out != null)

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 基于模板文件导出 word 文档,该方法支持03格式,但是此方法只能保留文档内容,不能保留文档中的样式和图片,建议将模板使用 07 的格式保存

*

* @param templatePath

* 模板文件的路径

* @param destFilePath

* 导出文件的存放路径,包含文件名,例如,E:/test/小区公告.doc

* @param data

* 用来替换文档中预定义的字符串,要求预定义的字符串与 data 中的 key 值要相同

*/

public static void export03WordByTemplate(String templatePath,

String destFilePath, MapString, String data) {

try {

WordExtractor doc = new WordExtractor(new FileInputStream(

templatePath));

String content = doc.getText();

for (String key : data.keySet()) {

content = content.replaceAll(key, data.get(key));

}

byte b[] = content.getBytes();

ByteArrayInputStream bais = new ByteArrayInputStream(b);

POIFSFileSystem fs = new POIFSFileSystem();

DirectoryEntry directory = fs.getRoot();

directory.createDocument("WordDocument", bais);

FileOutputStream ostream = new FileOutputStream(destFilePath);

fs.writeFilesystem(ostream);

bais.close();

ostream.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

public static void main(String[] args) throws Exception {

MapString, String maps = new HashMapString, String();

maps.put("appellation", "万达公寓业主:");

maps.put(

"main_body",

"输出的内容");

maps.put("date", "2013年1月23日");

exportWordByTemplate("E:/sss 2.docx", "E:/test/test.doc", maps);

}

}

"E:/sss 2.docx 模板存放的地址。

E:/test/test.doc 新生成的地址。

java 如何写excel

public static void main(String[] args) throws IOException {

//创建工作簿对象

HSSFWorkbook wb=new HSSFWorkbook();

//创建工作表对象

HSSFSheet sheet=wb.createSheet("我的工作表");

//创建绘图对象

HSSFPatriarch p=sheet.createDrawingPatriarch();

//创建单元格对象,批注插入到4行,1列,B5单元格

HSSFCell cell=sheet.createRow(4).createCell(1);

//插入单元格内容

cell.setCellValue(new HSSFRichTextString("批注"));

//获取批注对象

//(int dx1, int dy1, int dx2, int dy2, short col1, int row1, short col2, int row2)

//前四个参数是坐标点,后四个参数是编辑和显示批注时的大小.

HSSFComment comment=p.createComment(new HSSFClientAnchor(0,0,0,0,(short)3,3,(short)5,6));

//输入批注信息

comment.setString(new HSSFRichTextString("插件批注成功!插件批注成功!"));

//添加作者,选中B5单元格,看状态栏

comment.setAuthor("toad");

//将批注添加到单元格对象中

cell.setCellComment(comment);

//创建输出流

FileOutputStream out=new FileOutputStream("writerPostil.xls");

wb.write(out);

//关闭流对象

out.close();

}

Aspose.cell for Java+Aspose.PDF for Java 可以实现,但是网上Aspose的jar包貌似没破解的,.net的倒挺多.

「java表格排版」java自动排版

java表格排版的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java自动排版、java表格排版的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-22,除非注明,否则均为首码项目网原创文章,转载请注明出处。