1、背景
需要提供各种大小的office测试文件,如doc excel ppt , 大小从0.1M 到 150 M之间。
2、实现
寻找资源库, 未果。
使用代码填充实现, excel可行,利用POI
import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
//100KB 1M 5M 10M 20M 30M 40M 50M 100M 110M 120M 150M
private static final int cloumn_100K = 130;
private static final int cloumn_1M = 1300 *50;
private static final int cloumn_5M = 1300 * 5;
private static final int cloumn_10M = 1300 * 10;
private static final int row_1M = 120;
try {
//wFile = new FileOutputStream(file, false);
FileDialog fileDialog = new FileDialog(new Frame(),"选择文件夹...", FileDialog.SAVE);
// FileAccept acceptCondition=new FileAccept("xls");
//fileDialog.setFilenameFilter(acceptCondition);
fileDialog.setTitle("文件另存为");
fileDialog.setName("文件另存为");
fileDialog.setFile(file.getName());
fileDialog.doLayout();
fileDialog.show(true);
String path = fileDialog.getDirectory();
String fileName = fileDialog.getName();
String fileName1 = fileDialog.getFile();
if(path==null||path==""||fileName1==null||fileName1==""){
System.out.println("路径或文件名没有输入");
}
WritableWorkbook wbook = Workbook.createWorkbook((new File(path
+ "\\" + fileName1)));
WritableSheet sheet1 = wbook.createSheet(sheetName, 0);
System.out.println("开始循环插入.....");
//test produce big file
for (int i = 0; i < row_1M; i++) {
for(int j=0; j< cloumn_1M; j++) {
//title从表头的下一行开始
sheet1.addCell(new Label(i, j, "test content"+j));
}
}
System.out.println("插入完成");
wbook.write();
wbook.close();
System.out.println("保存完成");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
遇到的问题,由于jar包里面创建lable的参数行和列都是 int 类型,因此最大只能有 65536 ,会导致文件不能实现自己的无限制增大的效果。不知道为啥这个参数不是 long 类型。。。。
对于doc文件, 想法是通过代码填充字符串到txt文本,然后通过文件转换网站来转成 doc文件,
网址:https://cloudconvert.com/txt-to-doc
代码:
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
// int cap = 4 * 1024 ;
// long start = System.currentTimeMillis();
// RandomAccessFile r = new RandomAccessFile("D:\\test1.txt", "rw");
// r.setLength(cap);
// r.close();
// long duration = System.currentTimeMillis() - start;
// System.out.println(duration + " ms");
//256 字符, 512字节
String test = "据说有一位软件工程师,一位硬件工程师和一位项目经理同坐车参加研讨会。不幸在从盘山公路下山时坏在半路上了。于是两位工程师和一位经理就如何修车的问题展开了讨论。"
+ "硬件工程师说:“我可以用随身携带的瑞士军刀把车坏的部分拆下来,找出原因,排除故障。” "
+ "项目经理说:“根据经营管理学,应该召开会议,根据问题现状写出需求报告,制订计划,编写日程安排,逐步逼近,alpha测试,beta1测试和beta2测试解决问题。” "
+ "软件工程说:“咱们还是应该把车推回山顶再开下来,"
+ "看看问题是否重复发生。”程序猿追求MM不成,含泪追问:我在你眼里算什么?!MM答曰:真人版";
// 64 byte
String test2 = "you are a lion, really what that is a pen or what ever merry c.";
//100KB 1M 5M 10M 20M 30M 40M 50M 100M 110M 120M 150M
int BYTE_1M = 1024*2*8 ;
int INDEX_FOR_M = 102*2*8;
//变成1024 字节
StringBuffer sb = new StringBuffer();
for(int i=0; i< INDEX_FOR_M ; i++) {
sb.append(test2);
}
String filePath = "D:\\test_100k.txt";
FileOutputStream fos = null;
PrintWriter pw = null;
System.out.println(" 文件生成开始......");
try {
File testFile = new File(filePath);
if (!testFile.exists()) {
testFile.createNewFile();
System.out.println(" creat file filePath=" + filePath);
}
//FileInputStream fileInputStream = null;
fos = new FileOutputStream(testFile);
pw = new PrintWriter(fos);
String result = new String(sb.toString().getBytes(), "UTF-8");
pw.write(result.toCharArray());
pw.flush();
} catch (Exception e) {
// TODO: handle exception
} finally {
if (fos != null) {
fos.close();
}
if (pw != null) {
pw.close();
}
}
System.out.println(" 文件生成完成");
}
遇到的问题,刚开始使用中文填充,然后转换之后一直是乱码,增加了编码限制UTF-8也没有用处,由于时间原因,没有深入查明,所以直接改为填充英文字符。 另外就是转换网站不支持大文件转换,尝试了多次只能转换10M左右的文件,所以这个任务也是半吊子完成。
当然最后PPT还没有完成,算了,就骗骗他吧。。。