(上面一行暂时不会删除...)
由于上线系统要做国际化,英文翻译,需要把原来的porperties文件内容copy到excel中发给翻译公司去做,但由于properties文件太多,而又没有比较好的方法做批量的copy,最后想了想,想了个偷懒的方法:自己写个io来做个读写文件操作。
因为本身读取properties 文件很简单,读取properties文件中的key和value后,只需要把写入到新生成的excel的两个单元格中就ok了,由于系统调用poi的jar包来做excel文件导出,于是就试了试,这样一个简单的文件转换程序就完成了。只不过中间由于文件比较多,中间好几层文件夹,所以 中间还用了个递归。
具体实现时候还是baidu了下,poi不是很熟悉...
1、首先先把源码包copy一份到c盘的XXX目录里面,然后递归查找非properties文件,删除
public class PropertiesToExcel {
private static int num = 0;
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
System.out.println("begin time at " + new Date());
File file = new File("c:\\XXX");
transformProperitesToExcel(file);
System.out.println("end time at " + new Date());
export("d:\\test.xls");
}
public static void readPropertiesToExecl(File file, String path) throws IOException {
// TODO Auto-generated method stub
Properties p = new Properties();
InputStream inStream;
inStream = new FileInputStream(file);
p.load(inStream);
// System.out.println(p.getProperty("mail.debug"));
// System.out.println(p.isEmpty());
// p.setProperty("pom", "pom123");
// System.out.println(p.getProperty("pom"));
// p.clear();
// p = System.getProperties();
Enumeration e = p.propertyNames();
System.out.println(" 开始导出Excel文件 ");
XLSExport ee = new XLSExport(path);
int i = 0;
while (e.hasMoreElements()) {
String ele = (String) e.nextElement();
// System.out.println(ele + ":" + p.getProperty(ele));
ee.createRow(i);
ee.setCell(0, ele);
ee.setCell(1, p.getProperty(ele));
i++;
}
try {
ee.exportXLS(path); //导出成excel文件
System.out.println(" 导出Excel文件[成功] ");
} catch (Exception e1) {
System.out.println(" 导出Excel文件[失败] ");
e1.printStackTrace();
}
}
/**
* 读取file目录下所有文件
*/
public static void transformProperitesToExcel(File file) throws IOException {
if (file.isDirectory()) {
File[] subFile = file.listFiles();
if (subFile.length == 0) {
file.delete();
}
for (int i = 0; i < subFile.length; i++) {
transformProperitesToExcel(subFile[i]);
}
} else {
if ((!file.getName().endsWith(".PROPERTIES") && !file.getName().endsWith(".properties"))) {
// file.delete();
} else {
String path = file.getAbsolutePath().substring(0,
file.getAbsolutePath().lastIndexOf('\\'));
System.out.println(num + " 、转换文件:" + file.getAbsolutePath());
num++;
path = path + "\\packageExcel.xls"; //转换后相应的xls文件目录
// File excelFile = new File(path);
// if(!excelFile.canRead()){
// excelFile.createNewFile();
// }
readPropertiesToExecl(file, path);
// export(path);
}
}
}
public static void export(String file) {
System.out.println(" 开始导出Excel文件 ");
XLSExport e = new XLSExport(file);
try {
e.createRow(0);
e.setCell(0, " 编号 ");
e.setCell(1, " 名称 ");
e.exportXLS(file);
System.out.println(" 导出Excel文件[成功] ");
} catch (Exception e1) {
System.out.println(" 导出Excel文件[失败] ");
e1.printStackTrace();
}
}
}
//*************************生成excel文件工具类****************//
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/** */ /**
* 生成导出Excel文件对象
*
* @author John.Zhu
*
*/
public class XLSExport {
// 设置cell编码解决中文高位字节截断
// private static short XLS_ENCODING = HSSFWorkbook.ENCODING_UTF_16;
// 定制日期格式
private static String DATE_FORMAT = " m/d/yy " ; // "m/d/yy h:mm"
// 定制浮点数格式
private static String NUMBER_FORMAT = " #,##0.00 " ;
private String xlsFileName;
private HSSFWorkbook workbook;
private HSSFSheet sheet;
private HSSFRow row;
/** */ /**
* 初始化Excel
*
* @param fileName
* 导出文件名
*/
public XLSExport(String fileName) {
this .xlsFileName = fileName;
this .workbook = new HSSFWorkbook();
this .sheet = workbook.createSheet();
}
/** */ /**
* 导出Excel文件
*
* @throws XLSException
*/
public void exportXLS(String path) throws Exception {
try {
FileOutputStream fOut = new FileOutputStream(path);
workbook.write(fOut);
fOut.flush();
fOut.close();
} catch (FileNotFoundException e) {
throw new Exception( " 生成导出Excel文件出错! " , e);
} catch (IOException e) {
throw new Exception( " 写入Excel文件出错! " , e);
}
}
/** */ /**
* 增加一行
*
* @param index
* 行号
*/
public void createRow( int index) {
this .row = this .sheet.createRow(index);
}
/** */ /**
* 设置单元格
*
* @param index
* 列号
* @param value
* 单元格填充值
*/
public void setCell( int index, String value) {
HSSFCell cell = this .row.createCell(( short ) index);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(value);
}
/** */ /**
* 设置单元格
*
* @param index
* 列号
* @param value
* 单元格填充值
*/
public void setCell( int index, Calendar value) {
HSSFCell cell = this .row.createCell(( short ) index);
// cell.setEncoding(cell.ENCODING_UTF_16);
cell.setCellValue(value.getTime());
HSSFCellStyle cellStyle = workbook.createCellStyle(); // 建立新的cell样式
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat(DATE_FORMAT)); // 设置cell样式为定制的日期格式
cell.setCellStyle(cellStyle); // 设置该cell日期的显示格式
}
/** */ /**
* 设置单元格
*
* @param index
* 列号
* @param value
* 单元格填充值
*/
public void setCell( int index, int value) {
HSSFCell cell = this .row.createCell(( short ) index);
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(value);
}
/** */ /**
* 设置单元格
*
* @param index
* 列号
* @param value
* 单元格填充值
*/
public void setCell( int index, double value) {
HSSFCell cell = this .row.createCell(( short ) index);
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(value);
HSSFCellStyle cellStyle = workbook.createCellStyle(); // 建立新的cell样式
HSSFDataFormat format = workbook.createDataFormat();
cellStyle.setDataFormat(format.getFormat(NUMBER_FORMAT)); // 设置cell样式为定制的浮点数格式
cell.setCellStyle(cellStyle); // 设置该cell浮点数的显示格式
}
}