首页 > 编程知识 正文

java导出docx(java导出动态内容的excel)

时间:2023-12-02 21:07:33 阅读:311546 作者:KCYO

本文目录一览:

  • 1、java导出word表格
  • 2、请教:java从数据库获取数据导出docx,有多个表格,请问怎么导出?
  • 3、有谁知道用java语言怎么实现将word的doc格式文件转换为docx格式的
  • 4、java导出word,默认打开时doc或者docx格式

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从数据库获取数据导出docx,有多个表格,请问怎么导出?

第一步:如何用POI操作Excel

@Test

public void createXls() throws Exception{

//声明一个工作薄

HSSFWorkbook wb = new HSSFWorkbook();

//声明表

HSSFSheet sheet = wb.createSheet("第一个表");

//声明行

HSSFRow row = sheet.createRow(7);

//声明列

HSSFCell cel = row.createCell(3);

//写入数据

cel.setCellValue("你也好");

FileOutputStream fileOut = new FileOutputStream("d:/a/b.xls");

wb.write(fileOut);

fileOut.close();

}

第二步:导出指定数据库的所有表

分析:

1:某个数数据库有多少表,表名是什么?―――DataBaseMetadate.getMetadate().getTables(null,null,null,new String[]{Table}); - excel的文件名称。

2:对每一个表进行select * 操作。 - 每一个sheet的名称。

3:分析表结构,rs.getMetadate(); ResultSetMedated

4:多个列,列名是什么.- 字段名就是sheet的第一行信息。

5:获取每一行的数据 – 放到sheet第一行以后。

@Test

public void export() throws Exception{

//声明需要导出的数据库

String dbName = "focus";

//声明book

HSSFWorkbook book = new HSSFWorkbook();

//获取Connection,获取db的元数据

Connection con = DataSourceUtils.getConn();

//声明statemen

Statement st = con.createStatement();

//st.execute("use "+dbName);

DatabaseMetaData dmd = con.getMetaData();

//获取数据库有多少表

ResultSet rs = dmd.getTables(dbName,dbName,null,new String[]{"TABLE"});

//获取所有表名 - 就是一个sheet

ListString tables = new ArrayListString();

while(rs.next()){

String tableName = rs.getString("TABLE_NAME");

tables.add(tableName);

}

for(String tableName:tables){

HSSFSheet sheet = book.createSheet(tableName);

//声明sql

String sql = "select * from "+dbName+"."+tableName;

//查询数据

rs = st.executeQuery(sql);

//根据查询的结果,分析结果集的元数据

ResultSetMetaData rsmd = rs.getMetaData();

//获取这个查询有多少行

int cols = rsmd.getColumnCount();

//获取所有列名

//创建第一行

HSSFRow row = sheet.createRow(0);

for(int i=0;icols;i++){

String colName = rsmd.getColumnName(i+1);

//创建一个新的列

HSSFCell cell = row.createCell(i);

//写入列名

cell.setCellValue(colName);

}

//遍历数据

int index = 1;

while(rs.next()){

row = sheet.createRow(index++);

//声明列

for(int i=0;icols;i++){

String val = rs.getString(i+1);

//声明列

HSSFCell cel = row.createCell(i);

//放数据

cel.setCellValue(val);

}

}

}

con.close();

book.write(new FileOutputStream("d:/a/"+dbName+".xls"));

}

有谁知道用java语言怎么实现将word的doc格式文件转换为docx格式的

如果只是文字的话,直接读出来,然后用07的存回去,poi能实现。但是有目录什么的就麻烦了,03的读出来目录是一行代码,就容易出问题

我用的poi正好在纠结,最后没办法,用按键精灵一个个转的。有个word文档批处理软件貌似能做到,但是要收费。

下面的是我读取word文字的代码

03读取:

InputStream

is

=

new

FileInputStream(docfile);

WordExtractor

ex

=

new

WordExtractor(is);

text

=

ex.getText();

System.out.println(text);

07读取:

OPCPackage

opcPackage

=

POIXMLDocument.openPackage(docxfile.getAbsolutePath());

POIXMLTextExtractor

extractor

=

new

XWPFWordExtractor(opcPackage);

text

=

extractor.getText();

System.out.println(text);

最后:doc不一定是03的,也可能是07的

java导出word,默认打开时doc或者docx格式

到控制面板的功能和程序,点击2003,点击更改,修复,或者直接点击2003的安装程序进行修复也可以,这样默认就是2003,而docx只有2010才能打开。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。