最近在学校做一个兼职,需要每月进行一次,工作内容是:查询2016-2017年在学校期刊发表的相关论文,记录论文的作者、期刊编号、引用量等信息,然后向论文作者发一封论文引用情况的邮件,邮件模板是固定的,只要替换关键字即可。由于查询的论文有80篇左右,手动替换关键字太过费时,所以编写了一段程序实现,从Excel表读取已查询的论文信息,从TXT文本读取邮件模板,对邮件模板进行关键字替换,之后进行Word文本输出。
邮件模板如下,替换关键字为%Author%、%PaperTitle%、%volumeNum%、%issueNum%、%quotedNum%。
Dear Professor %Author%,
Your article titled as " %PaperTitle% " in XXXXXXXXXXX,volume %volumeNum% issue %issueNum%. The lates new citation in Web of Science is %quotedNum% up to January, 2018.
Now the jornal has been indexed by SCI. Please promote and enlarge the influence of your article as much as possible in the future.
Many thanks for your help!
Best regards,
Editorial Office of XXXXXXXXXXXXXX
Excel表内容如下。
首先编写TXT文本读取和输出的程序,如下所示。read()文本读取时,将文本每行添加到StringBuffer中,并为每行添加换行符。因为在读的过程中换行符会被去除掉。write()打开文本并将其写入文件。在方法完成时,需要close()关闭文件。
package excelOperate;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
public class TextFile {
public static String read(String fileName) {
StringBuilder sb = new StringBuilder();
try {
BufferedReader in = new BufferedReader(new FileReader(new File(fileName).getAbsolutePath()));
try {
String s;
while ((s = in.readLine()) != null) {
sb.append(s);
sb.append("\n");
}
} finally {
in.close();
}
} catch (IOException e) {
// TODO: handle exception
throw new RuntimeException(e);
}
return sb.toString();
}
public static void write(String fileName, String text) {
try {
PrintWriter out = new PrintWriter(new File(fileName).getAbsolutePath());
try {
out.print(text);
} finally {
out.close();
}
} catch (IOException e) {
// TODO: handle exception
throw new RuntimeException(e);
}
}
public static void main(String[] args){
}
}
之后编写Excel文件读取的程序,如下所示。
readCell()读取文件fileName的sheetNum工作表的第row+1行、第col+1列的单元格。首先设置Excel文件读取的编码格式——URF-8