commit a3928061e2caae9278b06e58245a2135fcafb1d8 Author: lishipeng <1572427111@qq.com> Date: Fri Mar 3 14:42:03 2023 +0800 first commit diff --git a/.idea/checkstyle-idea.xml b/.idea/checkstyle-idea.xml new file mode 100644 index 0000000..c370431 --- /dev/null +++ b/.idea/checkstyle-idea.xml @@ -0,0 +1,16 @@ + + + + 10.5.0 + JavaOnly + true + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..006aace --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__commons_codec_commons_codec_1_10.xml b/.idea/libraries/Maven__commons_codec_commons_codec_1_10.xml new file mode 100644 index 0000000..27424a1 --- /dev/null +++ b/.idea/libraries/Maven__commons_codec_commons_codec_1_10.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_apache_poi_poi_3_14.xml b/.idea/libraries/Maven__org_apache_poi_poi_3_14.xml new file mode 100644 index 0000000..4cbf0a1 --- /dev/null +++ b/.idea/libraries/Maven__org_apache_poi_poi_3_14.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_apache_poi_poi_ooxml_3_13.xml b/.idea/libraries/Maven__org_apache_poi_poi_ooxml_3_13.xml new file mode 100644 index 0000000..43e4ba9 --- /dev/null +++ b/.idea/libraries/Maven__org_apache_poi_poi_ooxml_3_13.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_apache_poi_poi_ooxml_schemas_3_13.xml b/.idea/libraries/Maven__org_apache_poi_poi_ooxml_schemas_3_13.xml new file mode 100644 index 0000000..57bbf23 --- /dev/null +++ b/.idea/libraries/Maven__org_apache_poi_poi_ooxml_schemas_3_13.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_apache_xmlbeans_xmlbeans_2_6_0.xml b/.idea/libraries/Maven__org_apache_xmlbeans_xmlbeans_2_6_0.xml new file mode 100644 index 0000000..cb0f76d --- /dev/null +++ b/.idea/libraries/Maven__org_apache_xmlbeans_xmlbeans_2_6_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_freemarker_freemarker_2_3_22.xml b/.idea/libraries/Maven__org_freemarker_freemarker_2_3_22.xml new file mode 100644 index 0000000..d930d57 --- /dev/null +++ b/.idea/libraries/Maven__org_freemarker_freemarker_2_3_22.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__stax_stax_api_1_0_1.xml b/.idea/libraries/Maven__stax_stax_api_1_0_1.xml new file mode 100644 index 0000000..0b13335 --- /dev/null +++ b/.idea/libraries/Maven__stax_stax_api_1_0_1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..a194cfe --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,93 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Android + + + + + + + + + + + + + 1.8 + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..e7f4e6d --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..7c95aa1 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,694 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + { + "keyToString": { + "SHARE_PROJECT_CONFIGURATION_FILES": "true", + "WebServerToolWindowFactoryState": "false", + "last_opened_file_path": "D:/OW/新建文件夹/toWord", + "vue.rearranger.settings.migration": "true" + } +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + project + + + + + + + + + + + + + + + + + + + + + + + 1483688327034 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..dd8fdc9 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# toWord + +此程序用于从Excel表中读取数据填到word模板中 + +详见[我的博客](http://www.jianshu.com/p/7dbb7d3e98ee) diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..2c6458f --- /dev/null +++ b/pom.xml @@ -0,0 +1,45 @@ + + + 4.0.0 + + com.wuchangming.cn + toWord + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 7 + 7 + + + + + jar + + toWord + toWord + + + + org.freemarker + freemarker + 2.3.22 + + + org.apache.poi + poi + 3.14 + + + org.apache.poi + poi-ooxml + 3.13 + + + + \ No newline at end of file diff --git a/src/main/java/DocumentHandler.java b/src/main/java/DocumentHandler.java new file mode 100644 index 0000000..2de847d --- /dev/null +++ b/src/main/java/DocumentHandler.java @@ -0,0 +1,64 @@ +import freemarker.template.Configuration; +import freemarker.template.Template; +import freemarker.template.TemplateException; + +import java.io.*; +import java.util.Map; + +/** + * Created by wuchangming on 17/1/6. + */ +public class DocumentHandler { + + + private Configuration configuration = null; + + public DocumentHandler() { + configuration = new Configuration(); + configuration.setDefaultEncoding("utf-8"); + } + + public Template getTemplate() throws IOException { + configuration.setDirectoryForTemplateLoading(new File("C:\\Users\\Reborn\\Desktop")); + Template t = null; + try { + // data.xml为要装载的模板 + t = configuration.getTemplate("test.xml"); + t.setEncoding("utf-8"); + } catch (IOException e) { + e.printStackTrace(); + } + return t; + } + + public Writer getWriter(String wordName){ + String savePath = "C:\\Users\\Reborn\\Desktop\\"; + File file = new File(savePath+"upload"); + if(!file.exists()){ + file.mkdirs(); + } + File outFile = new File(savePath+"upload/"+wordName+".doc"); + Writer out = null; + try { + out = new BufferedWriter(new OutputStreamWriter( + new FileOutputStream(outFile), "utf-8")); + } catch (Exception e1) { + e1.printStackTrace(); + } + return out; + } + + + public void createDoc(Template t,Map dataMap,Writer out) { + try { + t.process(dataMap, out); + out.close(); + } catch (TemplateException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + +} diff --git a/src/main/java/ReadExcelTable.java b/src/main/java/ReadExcelTable.java new file mode 100644 index 0000000..845c21a --- /dev/null +++ b/src/main/java/ReadExcelTable.java @@ -0,0 +1,118 @@ +/** + * Created by wuchangming on 17/1/6. + */ + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; + +import java.io.FileInputStream; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * 读取上篇中的xls文件的内容,并打印出来 + * + * @author Administrator + */ +public class ReadExcelTable { + + + //通过对单元格遍历的形式来获取信息 ,这里要判断单元格的类型才可以取出值 + public static List readTable() throws Exception { + Map map = new HashMap(); + List list = new ArrayList(); +// map.put("308", "招商银行"); // 招行 +// map.put("306", "广发银行"); // 广发 +// map.put("303", "光大银行"); // 光大 +// map.put("304", "华夏银行"); // 华夏 +// map.put("105", "建设银行"); // 建行 +// map.put("305", "民生银行"); // 民生 +// map.put("103", "农业银行"); // 农行 +// map.put("310", "浦发银行"); // 浦发 +// map.put("309", "兴业银行"); // 兴业 +// map.put("104", "中国银行"); // 中行 +// map.put("302", "中信银行"); // 中信 +// map.put("102", "工商银行"); // 工行 +// map.put("301", "交通银行"); // 交行 +// map.put("4105840", "平安银行"); // 平安 +// map.put("403", "邮政储蓄银行"); // 邮储 + InputStream ips = new FileInputStream("C:\\Users\\Reborn\\Desktop\\test.xlsx"); + XSSFWorkbook wb = new XSSFWorkbook(ips); + Sheet sheet1 = wb.getSheetAt(0); + for (Row row : sheet1) { + + String company = row.getCell(0).getStringCellValue(); + String name = row.getCell(1).getStringCellValue(); + row.getCell(2).setCellType(Cell.CELL_TYPE_STRING); + String identity = row.getCell(2).getStringCellValue(); + row.getCell(3).setCellType(Cell.CELL_TYPE_STRING); + String time = row.getCell(3).getStringCellValue(); + String things = row.getCell(4).getStringCellValue(); + String personName = row.getCell(5).getStringCellValue(); + row.getCell(6).setCellType(Cell.CELL_TYPE_STRING); + String date = row.getCell(6).getStringCellValue(); + + + Map dataMap = new HashMap(); + dataMap.put("company", company); + dataMap.put("name", name); + dataMap.put("identity", identity); + dataMap.put("time", time); + dataMap.put("things", things); + dataMap.put("personName", personName); + dataMap.put("shouquanren", name); + dataMap.put("date", date); + list.add(dataMap); + +// for (Cell cell : row) { +// switch (cell.getCellType()) { +// case HSSFCell.CELL_TYPE_BOOLEAN: +// //得到Boolean对象的方法 +// System.out.print(cell.getBooleanCellValue()+" "); +// break; +// case HSSFCell.CELL_TYPE_NUMERIC: +// //先看是否是日期格式 +// if(HSSFDateUtil.isCellDateFormatted(cell)){ +// //读取日期格式 +// System.out.print(cell.getDateCellValue()+" "); +// }else{ +// //读取数字 +// System.out.print(cell.getNumericCellValue()+" "); +// } +// break; +// case HSSFCell.CELL_TYPE_FORMULA: +// //读取公式 +// System.out.print(cell.getCellFormula()+" "); +// break; +// case HSSFCell.CELL_TYPE_STRING: +// //读取String +// System.out.print(cell.getRichStringCellValue().toString()+" "); +// break; +// default: +// System.out.println(); +// } +// +// } + } + return list; + + } + +// //直接抽取excel中的数据 +// public static void extractor() throws Exception{ +// InputStream ips=new FileInputStream("d://test2-1.xls"); +// HSSFWorkbook wb=new HSSFWorkbook(ips); +// ExcelExtractor ex=new ExcelExtractor(wb); +// ex.setFormulasNotResults(true); +// ex.setIncludeSheetNames(false); +// String text=ex.getText(); +// System.out.println(text); +// } + + +} diff --git a/src/main/java/WordTest.java b/src/main/java/WordTest.java new file mode 100644 index 0000000..a2b8b2a --- /dev/null +++ b/src/main/java/WordTest.java @@ -0,0 +1,34 @@ +import freemarker.template.Configuration; +import freemarker.template.Template; + +import java.io.*; +import java.util.HashMap; +import java.util.Map; + +public class WordTest { + public static void genWord(){ + try { + Map dataMap = new HashMap<>(); + dataMap.put("name", "张三"); + Configuration configuration = new Configuration(); + configuration.setDefaultEncoding("utf-8"); + //.xml 模板文件所在目录 + configuration.setDirectoryForTemplateLoading(new File("H:\\workFile\\genword\\template")); + // 输出文档路径及名称 + File outFile = new File("H:\\workFile\\genword\\word\\test.doc"); + //以utf-8的编码读取模板文件 + Template t = configuration.getTemplate("test.xml","utf-8"); + Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"),10240); + t.process(dataMap, out); + out.close(); + System.out.println("生成成功"); + }catch (Exception e){ + e.printStackTrace(); + System.out.println("生成失败"); + } + } + + public static void main(String[] args) { + genWord(); + } +} diff --git a/src/main/java/excelToWord.java b/src/main/java/excelToWord.java new file mode 100644 index 0000000..f7f77ca --- /dev/null +++ b/src/main/java/excelToWord.java @@ -0,0 +1,41 @@ +import freemarker.template.Configuration; +import freemarker.template.Template; + +import java.io.*; +import java.util.List; +import java.util.Map; + +/** + * Created by wuchangming on 17/1/6. + */ +public class excelToWord { + public static void main(String[] args) throws Exception { + personnelImportWord(); + } + + + + private static void personnelImportWord() throws Exception{ + DocumentHandler dh = new DocumentHandler(); +// Template t = dh.getTemplate(); + + List list=ReadExcelTable.readTable(); + for (Map map : list){ +// Writer out = dh.getWriter((String)map.get("name")); +// dh.createDoc(t,map, out); + Configuration configuration = new Configuration(); + configuration.setDefaultEncoding("utf-8"); + //.xml 模板文件所在目录 + configuration.setDirectoryForTemplateLoading(new File("C:\\Users\\Reborn\\Desktop\\temp")); + // 输出文档路径及名称 + File outFile = new File("C:\\Users\\Reborn\\Desktop\\"+map.get("name")+".doc"); + //以utf-8的编码读取模板文件 + Template t = configuration.getTemplate("test.xml","utf-8"); + Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"),10240); + t.process(map, out); + out.close(); + System.out.println("生成成功"); + } + + } +} diff --git a/target/classes/DocumentHandler.class b/target/classes/DocumentHandler.class new file mode 100644 index 0000000..42bb17f Binary files /dev/null and b/target/classes/DocumentHandler.class differ diff --git a/target/classes/ReadExcelTable.class b/target/classes/ReadExcelTable.class new file mode 100644 index 0000000..2c77965 Binary files /dev/null and b/target/classes/ReadExcelTable.class differ diff --git a/target/classes/WordTest.class b/target/classes/WordTest.class new file mode 100644 index 0000000..fbd110b Binary files /dev/null and b/target/classes/WordTest.class differ diff --git a/target/classes/excelToWord.class b/target/classes/excelToWord.class new file mode 100644 index 0000000..ea2d6e6 Binary files /dev/null and b/target/classes/excelToWord.class differ diff --git a/toWord.iml b/toWord.iml new file mode 100644 index 0000000..218dab3 --- /dev/null +++ b/toWord.iml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file