原創(chuàng)/朱季謙
這款工具是筆者在2018年初開發(fā)完成的,時(shí)隔兩載,偶然想起這款小工具,于是,決定將其開源,若有人需要做類似Java批處理實(shí)現(xiàn)整理文檔的工具,可參考該工具邏輯思路來實(shí)現(xiàn)。
該工具是運(yùn)行在windos系統(tǒng)上的,基于bat腳本與jar包形式協(xié)同運(yùn)行。當(dāng)時(shí)開發(fā)該工具的背景是,需要每天定時(shí)處理大批量的對(duì)賬單txt文本信息,將其統(tǒng)一整合到一張Excel文檔上,供會(huì)計(jì)人員獲取。在沒有該工具之前,項(xiàng)目組上的會(huì)計(jì)人員,需要每天手動(dòng)打開大量txt文本,并從每份txt文檔里,復(fù)制所需信息,然后將獲取到的信息再復(fù)制到一份當(dāng)日的Excel文檔里。這個(gè)過程根據(jù)對(duì)賬單數(shù)量而定,若數(shù)量過多,手動(dòng)整理就需花費(fèi)大量時(shí)間,且過程重復(fù)操作?;谶@個(gè)原因,當(dāng)時(shí)就開發(fā)了這款小工具,供會(huì)計(jì)人員使用,其帶來的效果是,節(jié)省了大量整理時(shí)間:會(huì)計(jì)人員只需把每天郵件收到的批量對(duì)賬單txt文件,統(tǒng)一放到指定目錄下,點(diǎn)擊start啟動(dòng)腳本,就可一鍵自動(dòng)批量處理完成。
這個(gè)工具在當(dāng)時(shí)的工作環(huán)境下,是行之有效的,但若換到另一種領(lǐng)域或者環(huán)境,還需二次開發(fā)做修改。工具整體實(shí)現(xiàn)的邏輯并不復(fù)雜,筆者只提供一種解決文檔重復(fù)整理工作的小思路,僅做參考學(xué)習(xí)之用,畢竟,解決問題的本質(zhì)不在于工具,而在于思路。
下面,就圍繞著業(yè)務(wù)與具體實(shí)現(xiàn)來結(jié)束該自動(dòng)處理工具。
整體結(jié)構(gòu)如下:
1.對(duì)賬單:將同類型對(duì)賬單批量放入到對(duì)賬單文件夾中,同類,即格式幾乎一樣,但數(shù)據(jù)不一樣,如下所示:
2.對(duì)賬單集處理結(jié)果:批量處理獲取到的數(shù)據(jù),會(huì)統(tǒng)一寫入到一份自動(dòng)生成的Excel文檔里,該文檔存放在“對(duì)賬單集處理結(jié)果”目錄底下;
3.Auto.jar:由Java語言開發(fā)的jar包,通過循環(huán)讀取各txt文本數(shù)據(jù),從讀取文本指定字段數(shù)據(jù),截取其名字與對(duì)應(yīng)保證金、可用資金,再寫入到自動(dòng)生成的Excel文檔里。
4.CopyName.bat:bat腳本,將本目錄下的txt文件名批量寫入到“對(duì)賬單批量名字集合.txt”;
CopyName.bat如下:
1 @dir /a-d /b *.txt>對(duì)賬單批量名字集合.txt
5.Start.bat:bat腳本,主要實(shí)現(xiàn)是,將CopyName.bat和“對(duì)賬單批量名字集合.txt”都復(fù)制到“對(duì)賬單”目錄,然后執(zhí)行CopyName.bat,將該目錄底下的所有.txt后綴的文件名,寫入到“對(duì)賬單批量名字集合.txt”,再啟動(dòng)Auto.jar包,該jar會(huì)去“對(duì)賬單批量名字集合.txt”獲取所在目錄下各txt文檔名字,再根據(jù)這些名字去讀取對(duì)應(yīng)的txt文檔。
Start.bat主要代碼如下:
1 @echo off
2 copy /y CopyName.bat 對(duì)賬單
3 copy /y 對(duì)賬單批量名字集合.txt 對(duì)賬單
4 cd D:\批量處理對(duì)賬單\對(duì)賬單
5 call CopyName.bat
6 java -jar D:\批量處理對(duì)賬單\Auto.jar
綜上,業(yè)務(wù)人員只需把對(duì)賬單統(tǒng)一放入到“對(duì)賬單”目錄下:
點(diǎn)擊Start.bat啟動(dòng),就可得到以下指定數(shù)據(jù)的統(tǒng)一獲?。?/p>
接下來,就具體分享一下Java部分的邏輯實(shí)現(xiàn):
代碼結(jié)構(gòu)
以maven進(jìn)行jar依賴,主要有Datas、ExportExcelBase、ExportExcleClient、PutExcel四個(gè)類。
1.引入依賴
1 <dependencies>
2 <dependency>
3 <groupId>org.apache.poi</groupId>
4 <artifactId>poi</artifactId>
5 <version>3.10-FINAL</version>
6 </dependency>
7 <dependency>
8 <groupId>org.projectlombok</groupId>
9 <artifactId>lombok</artifactId>
10 <version>1.18.2</version>
11 </dependency>
12 </dependencies>
2.設(shè)置導(dǎo)出基本類,根據(jù)需生成的Excel展示數(shù)據(jù)設(shè)置,筆者案例里只需展示“組合名”,“保證金占用金額”,“可用資金額”三個(gè)字段,故只需設(shè)置name,margin,avaFunds來接受獲取到的值;
1 package com.put.data;
2 import lombok.Data;
3 ?
4 /**
5 * 導(dǎo)出數(shù)據(jù)類
6 * @author zhujiqian
7 * @date 2020/10/27 20:09
8 */
9 @Data
10 public class Datas {
11 //名字
12 private String name;
13 //保證金
14 private String margin;
15 //可用資金
16 private String avaFunds;
17
18 public Datas(String name, String margin, String avaFunds) {
19 this.name = name;
20 this.margin = margin;
21 this.avaFunds = avaFunds;
22 }
23 ?
24 }
3.設(shè)置Excel表格生成類
1 package com.put.put;
2 ?
3 import org.apache.poi.hssf.usermodel.*;
4 import org.apache.poi.hssf.util.Region;
5 ?
6 import java.io.File;
7 import java.io.FileNotFoundException;
8 import java.io.FileOutputStream;
9 import java.io.IOException;
10 ?
11 /**
12 * HSSF - 提供讀寫Microsoft Excel格式檔案的功能。
13 *
14 * XSSF - 提供讀寫Microsoft Excel OOXML格式檔案的功能。
15 *
16 * @author zhujiqian
17 * @date 2020/10/27 20:33
18 */
19 public class ExportExcelBase {
20 private HSSFWorkbook hwb=null;
21 private HSSFSheet sheet=null;
22 public ExportExcelBase(HSSFWorkbook hwb,HSSFSheet sheet){
23 this.hwb=hwb;
24 this.sheet=sheet;
25 }
26 public HSSFWorkbook getHwb() {
27 return hwb;
28 }
29 public void setHwb(HSSFWorkbook hwb) {
30 this.hwb = hwb;
31 }
32 public HSSFSheet getSheet() {
33 return sheet;
34 }
35 public void setSheet(HSSFSheet sheet) {
36 this.sheet = sheet;
37 }
38 ?
39 /**
40 * 創(chuàng)建設(shè)置表格頭
41 */
42 public void createNormalHead(String headString,int colSum){
43 //創(chuàng)建表格標(biāo)題行,第一行
44 HSSFRow row=this.sheet.createRow(0);
45 //創(chuàng)建指定行的列,第一列
46 HSSFCell cell=row.createCell(0);
47 //設(shè)置標(biāo)題行默認(rèn)行高
48 row.setHeight((short) 500);
49 //設(shè)置表格內(nèi)容類型:0-數(shù)值型;1-字符串;2-公式型;3-空值;4-布爾型;5-錯(cuò)誤
50 cell.setCellType(1);
51 //設(shè)置表格標(biāo)題內(nèi)容
52 cell.setCellValue(new HSSFRichTextString(headString));
53 // 指定合并區(qū)域
54 this.sheet.addMergedRegion(new Region(0, (short)0, 0, (short)colSum));
55 // 定義單元格格式,添加單元格表樣式,并添加到工作簿
56 HSSFCellStyle cellStyle=this.hwb.createCellStyle();
57 // 設(shè)置單元格水平對(duì)齊居中類型
58 cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
59 // 指定單元格垂直居中對(duì)齊
60 cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
61 // 指定單元格自動(dòng)換行
62 cellStyle.setWrapText(true);
63 //設(shè)置字體
64 HSSFFont font=this.hwb.createFont();
65 font.setBoldweight((short) 700);
66 font.setFontName("宋體");
67 font.setFontHeight((short) 300);
68 cellStyle.setFont(font);
69 cell.setCellStyle(cellStyle);
70 }
71 ?
72 /**
73 *表單第二行
74 * @param params
75 * @param colSum
76 */
77 public void createNormalTwoRow(String[] params,int colSum){
78 HSSFRow row1=this.sheet.createRow(1);
79 row1.setHeight((short) 300);
80 HSSFCell cell2=row1.createCell(0);
81 cell2.setCellType(1);
82 cell2.setCellValue(new HSSFRichTextString("統(tǒng)計(jì)時(shí)間"+params[0]+"至"+params[1]));
83 this.sheet.addMergedRegion(new Region(1, (short) 0,1,(short)colSum));
84 HSSFCellStyle cellStyle=this.hwb.createCellStyle();
85 cellStyle.setAlignment((short) 2);
86 cellStyle.setVerticalAlignment((short) 1);
87 cellStyle.setWrapText(true);
88 HSSFFont font=this.hwb.createFont();
89 font.setBoldweight((short) 700);
90 font.setFontName("宋體");
91 font.setFontHeight((short) 250);
92 cellStyle.setFont(font);
93 cell2.setCellStyle(cellStyle);
94 }
95 ?
96 ?
97 /**
98 * 表單內(nèi)容
99 * @param wb
100 * @param row
101 * @param col
102 * @param align
103 * @param val
104 */
105 public void cteateCell(HSSFWorkbook wb, HSSFRow row, int col, short align, String val) {
106 HSSFCell cell = row.createCell(col);
107 cell.setCellType(1);
108 cell.setCellValue(new HSSFRichTextString(val));
109 HSSFCellStyle cellstyle = wb.createCellStyle();
110 cellstyle.setAlignment(align);
111 cell.setCellStyle(cellstyle);
112 }
113 ?
114 ?
115 /**
116 * 文檔輸出流
117 * @param fileName
118 */
119 public void outputExcle(String fileName) {
120 FileOutputStream fos = null;
121 try {
122 fos = new FileOutputStream(new File(fileName));
123 this.hwb.write(fos);
124 fos.close();
125 } catch (FileNotFoundException var4) {
126 var4.printStackTrace();
127 } catch (IOException var5) {
128 var5.printStackTrace();
129 }
130 }
131 }
4.設(shè)置Excel根據(jù)數(shù)據(jù)導(dǎo)出類
1 package com.put.put;
2 ?
3 ?
4 import com.put.data.Datas;
5 import org.apache.poi.hssf.usermodel.*;
6 ?
7 import java.text.SimpleDateFormat;
8 import java.util.Date;
9 import java.util.List;
10 ?
11 /**
12 * TODO
13 *
14 * @author zhujiqian
15 * @date 2020/10/27 20:24
16 */
17 public class ExportExcleClient {
18 private HSSFWorkbook hwb=null;
19 private HSSFSheet sheet=null;
20 ?
21 ExportExcelBase exportExcel = null;
22 SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
23 ?
24 public ExportExcleClient() {
25 this.hwb = new HSSFWorkbook();
26 this.exportExcel = new ExportExcelBase(this.hwb, this.sheet);
27 }
28 ?
29 /**
30 * 導(dǎo)出Excel
31 * @return
32 */
33 public String exportExcel() {
34 String a = this.df.format(new Date()) + "對(duì)賬單集合.xls";
35 String b = "D:\\批量處理對(duì)賬單\\對(duì)賬單集處理結(jié)果\\" + a;
36 this.exportExcel.outputExcle(b);
37 return b;
38 }
39 ?
40 /**
41 * 設(shè)置導(dǎo)出格式
42 * @param data
43 * @return
44 */
45 public String alldata(List<Datas> data) {
46 if (data.size() != 0) {
47 this.sheet = this.exportExcel.getHwb().createSheet("對(duì)賬單集合");
48 this.exportExcel.setSheet(this.sheet);
49 int number = 2;
50 ?
51 for(int i = 0; i < number; ++i) {
52 this.sheet.setColumnWidth(i, 8000);
53 }
54 ?
55 HSSFCellStyle cellStyle = this.hwb.createCellStyle();
56 cellStyle.setAlignment((short)2);
57 cellStyle.setVerticalAlignment((short)1);
58 cellStyle.setWrapText(true);
59 HSSFFont font = this.hwb.createFont();
60 font.setBoldweight((short)700);
61 font.setFontName("宋體");
62 font.setFontHeight((short)200);
63 cellStyle.setFont(font);
64 this.exportExcel.createNormalHead("對(duì)賬單整合表", number);
65 String[] params = new String[]{this.df.format(new Date()), this.df.format(new Date())};
66 this.exportExcel.createNormalTwoRow(params, number);
67 HSSFRow row2 = this.sheet.createRow(2);
68 HSSFCell cell0 = row2.createCell(0);
69 cell0.setCellStyle(cellStyle);
70 cell0.setCellValue(new HSSFRichTextString("組合名"));
71 HSSFCell cell1 = row2.createCell(1);
72 cell1.setCellStyle(cellStyle);
73 cell1.setCellValue(new HSSFRichTextString("保證金占用金額"));
74 HSSFCell cell2 = row2.createCell(2);
75 cell2.setCellStyle(cellStyle);
76 cell2.setCellValue(new HSSFRichTextString("可用資金額"));
77 ?
78 for(int i = 0; i < data.size(); ++i) {
79 System.out.println("==============" + ((Datas)data.get(i)).getName() + " " + ((Datas)data.get(i)).getMargin() + " " + ((Datas)data.get(i)).getAvaFunds());
80 HSSFRow row = this.sheet.createRow((short)i + 3);
81 this.exportExcel.cteateCell(this.hwb, row, 0, (short)6, ((Datas)data.get(i)).getName());
82 this.exportExcel.cteateCell(this.hwb, row, 1, (short)6, ((Datas)data.get(i)).getMargin());
83 this.exportExcel.cteateCell(this.hwb, row, 2, (short)6, ((Datas)data.get(i)).getAvaFunds());
84 }
85 }
86 ?
87 return "";
88 }
89 }
5.批量讀取txt文本截取指定數(shù)據(jù)類
1 package com.put;
2 ?
3 import com.put.data.Datas;
4 import com.put.put.ExportExcleClient;
5 import java.io.*;
6 import java.util.ArrayList;
7 import java.util.List;
8 ?
9 /**
10 * TODO
11 *
12 * @author zhujiqian
13 * @date 2020/10/27 20:08
14 */
15 public class PutExcel {
16 public PutExcel() {
17 }
18 public static List<String> readTxtFile1(String TxtName, String filePath) {
19 ArrayList list = new ArrayList();
20 try {
21 System.out.println("該組合:" + TxtName);
22 File file = new File(filePath);
23 if (file.isFile() && file.exists()) {
24 InputStreamReader read = new InputStreamReader(new FileInputStream(file), "GBK");
25 BufferedReader bufferedReader = new BufferedReader(read);
26 String lineTxt = null;
27 ?
28 while(true) {
29 do {
30 if ((lineTxt = bufferedReader.readLine()) == null) {
31 read.close();
32 return list;
33 }
34 } while(!lineTxt.contains("持倉保證金:") && !lineTxt.contains("保證金占用:") && !lineTxt.contains("保證金占用 Margin Occupied:") && !lineTxt.contains("保證金占用 Margin Occupied:"));
35 String ZiJin;
36 int a;
37 String b;
38 int c;
39 String d;
40 int e;
41 int f;
42 String E;
43 if (lineTxt.contains("持倉保證金:")) {
44 ZiJin = lineTxt.replace(" ", "");
45 a = ZiJin.indexOf("持");
46 b = ZiJin.substring(a);
47 c = b.indexOf(".");
48 d = b.substring(0, c + 3);
49 e = d.indexOf(":");
50 f = d.length();
51 E = d.substring(e + 1, f);
52 list.add(E);
53 } else if (lineTxt.contains("保證金占用:")) {
54 ZiJin = lineTxt.replace(" ", "");
55 a = ZiJin.indexOf("保");
56 b = ZiJin.substring(a);
57 c = b.indexOf(".");
58 d = b.substring(0, c + 3);
59 e = d.indexOf(":");
60 f = d.length();
61 E = d.substring(e + 1, f);
62 list.add(E);
63 } else if (lineTxt.contains("保證金占用 Margin Occupied:")) {
64 ZiJin = lineTxt.replace(" ", "");
65 a = ZiJin.indexOf("保");
66 b = ZiJin.substring(a);
67 c = b.indexOf(".");
68 d = b.substring(0, c + 3);
69 e = d.indexOf(":");
70 f = d.length();
71 E = d.substring(e + 1, f);
72 list.add(E);
73 } else if (lineTxt.contains("保證金占用 Margin Occupied:")) {
74 ZiJin = lineTxt.replace(" ", "");
75 a = ZiJin.indexOf("保");
76 b = ZiJin.substring(a);
77 c = b.indexOf(".");
78 d = b.substring(0, c + 3);
79 e = d.indexOf(":");
80 f = d.length();
81 E = d.substring(e + 1, f);
82 list.add(E);
83 }
84 }
85 } else {
86 System.out.println("找不到指定的文件");
87 }
88 } catch (Exception var16) {
89 System.out.println("讀取文件內(nèi)容出錯(cuò)");
90 var16.printStackTrace();
91 }
92 return list;
93 }
94 public static List<String> readTxtFile2(String TxtName, String filePath) {
95 ArrayList list = new ArrayList();
96 ?
97 try {
98 ?
99 System.out.println("該組合:" + TxtName);
100 File file = new File(filePath);
101 if (file.isFile() && file.exists()) {
102 InputStreamReader read = new InputStreamReader(new FileInputStream(file), "GBK");
103 BufferedReader bufferedReader = new BufferedReader(read);
104 String lineTxt = null;
105 ?
106 while(true) {
107 do {
108 if ((lineTxt = bufferedReader.readLine()) == null) {
109 read.close();
110 return list;
111 }
112 } while(!lineTxt.contains("可用資金:") && !lineTxt.contains("可用資金 Fund Avail.:") && !lineTxt.contains("可用資金 Fund Avail.:"));
113 String ZiJin;
114 int a;
115 String b;
116 int c;
117 String d;
118 int e;
119 int f;
120 String E;
121 if (lineTxt.contains("可用資金:")) {
122 ZiJin = lineTxt.replace(" ", "");
123 ZiJin = lineTxt.replace(" ", "");
124 a = ZiJin.indexOf("可");
125 b = ZiJin.substring(a);
126 c = b.indexOf(".");
127 d = b.substring(0, c + 3);
128 e = d.indexOf(":");
129 f = d.length();
130 E = d.substring(e + 1, f);
131 list.add(E);
132 } else if (lineTxt.contains("可用資金 Fund Avail.:")) {
133 ZiJin = lineTxt.replace(" ", "");
134 ZiJin = lineTxt.replace(" ", "");
135 a = ZiJin.indexOf("可");
136 b = ZiJin.substring(a);
137 c = b.lastIndexOf(".");
138 d = b.substring(0, c + 3);
139 e = d.indexOf(":");
140 f = d.length();
141 E = d.substring(e + 1, f);
142 list.add(E);
143 } else if (lineTxt.contains("可用資金 Fund Avail.:")) {
144 lineTxt.replace(" ", "");
145 ZiJin = lineTxt.replace(" ", "");
146 a = ZiJin.indexOf("可");
147 b = ZiJin.substring(a);
148 c = b.lastIndexOf(".");
149 d = b.substring(0, c + 3);
150 e = d.indexOf(":");
151 f = d.length();
152 E = d.substring(e + 1, f);
153 list.add(E);
154 }
155 }
156 } else {
157 System.out.println("找不到指定的文件");
158 }
159 } catch (Exception var16) {
160 System.out.println("讀取文件內(nèi)容出錯(cuò)");
161 var16.printStackTrace();
162 }
163 ?
164 return list;
165 }
166 ?
167 public static void main(String[] argv) {
168 String path = "D:\\批量處理對(duì)賬單\\對(duì)賬單\\對(duì)賬單批量名字集合.txt";
169 List<String> nums = writeToDat(path);
170 List<Datas> listData = new ArrayList();
171 for(int i = 0; i < nums.size(); ++i) {
172 if (!(nums.get(i)).equals("對(duì)賬單批量名字集合.txt")) {
173 listData.add(ZuHe(nums.get(i)));
174 System.out.println("--------==========" + ZuHe(nums.get(i)));
175 }
176 }
177 ?
178 System.out.println("-----------" + listData);
179 ExportExcleClient client = new ExportExcleClient();
180 client.alldata(listData);
181 String url = client.exportExcel();
182 System.out.println(url);
183 }
184 ?
185 public static Datas ZuHe(String TxtName) {
186 String address = "D:\\批量處理對(duì)賬單\\對(duì)賬單\\" + TxtName;
187 List<String> r1 = readTxtFile1(TxtName, address);
188 List<String> r2 = readTxtFile2(TxtName, address);
189 int c = TxtName.indexOf(".");
190 String txt = TxtName.substring(0, c);
191 System.out.println(txt);
192 System.out.println(r1);
193 Datas d = null;
194 if (r1.isEmpty() && r2.isEmpty()) {
195 if (r1.isEmpty() && r2.isEmpty()) {
196 System.out.println(txt + "--" + r1 + "---" + r2);
197 d = new Datas(txt, "無", "無");
198 }
199 } else {
200 d = new Datas(txt, r1.get(0), r2.get(0));
201 }
202 return d;
203 }
204 public static List<String> writeToDat(String path) {
205 File file = new File(path);
206 ArrayList list = new ArrayList();
207 try {
208 String encoding = "GBK";
209 InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);
210 BufferedReader bw = new BufferedReader(read);
211 String line = null;
212 ?
213 while((line = bw.readLine()) != null) {
214 list.add(line);
215 }
216 bw.close();
217 } catch (IOException var7) {
218 var7.printStackTrace();
219 }
220 return list;
221 }
222 }
以上的代碼,大部分都是在2018年左右寫成,現(xiàn)再閱讀,代碼風(fēng)格甚為稚嫩。我沒有做大的修改,原因是,想要留住這些代碼最初的樣子,就像留住剛畢業(yè)那會(huì)的記憶一般。整體實(shí)現(xiàn)邏輯并不算復(fù)雜,但再簡(jiǎn)單的東西,能解決問題,都是值得分享的東西。在此基礎(chǔ)上,還可繼續(xù)完善與擴(kuò)展,給需要用到的業(yè)務(wù)人員帶來方便。
這是我開源的第一個(gè)小工具,以此為勵(lì),在以后的日子里,要更加深入地學(xué)習(xí),并將所學(xué)與所得,多多分享。在我看來,輸入的東西,不一定是自己的,但輸出的,一定是自己的。
這,就是我喜歡輸出的原因之一。文章來源:http://www.zghlxwxcb.cn/news/detail-779829.html
最后,附上第一個(gè)github源碼鏈接:https://github.com/z924931408/auto-put-tool文章來源地址http://www.zghlxwxcb.cn/news/detail-779829.html
到了這里,關(guān)于開源一套文本處理工具:Java+Bat腳本實(shí)現(xiàn)自動(dòng)批量處理對(duì)賬單工具的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!