private void saveAsExcel(BizContent biz,OrderInfo info){
ArrayList<String> dataStr = new ArrayList<String>();
StringBuffer strBuff = new StringBuffer();
//添加數據
strBuff.append(info.getEffectiveDate()).append(",")
dataStr.add(strBuff.toString());
ArrayList<String> fieldName = new ArrayList<String>();
//添加列名
fieldName.add("生效日期");
File file =new File("c:\\zhongan.xls");
if(!file.exists()){
System.out.println("不存在");
try {
System.out.println(file.createNewFile());
file.createNewFile();
ExcelFileGenerator generator = new ExcelFileGenerator(fieldName,
dataStr);
OutputStream os=new FileOutputStream(file);
generator.expordExcel(os);
} catch (Exception e) {
System.out.println("error");
}
}else{
try {
POIFSFileSystem ps=new POIFSFileSystem(new FileInputStream("c:\\zhongan.xls")); //使用POI提供的方法得到excel的信息
HSSFWorkbook wb=new HSSFWorkbook(ps);
HSSFSheet sheet=wb.getSheetAt(0); //獲取到工作表,因為一個excel可能有多個工作表
HSSFRow row=sheet.getRow(0); //獲取第一行(excel中的行默認從0開始,所以這就是為什么,一個excel必須有字段列頭),即,字段列頭,便於賦值
System.out.println(sheet.getLastRowNum()+" "+row.getLastCellNum()); //分別得到最后一行的行號,和一條記錄的最后一個單元格
FileOutputStream out=new FileOutputStream("c:\\zhongan.xls"); //向c:\\zhongan.xls中寫數據
row=sheet.createRow((short)(sheet.getLastRowNum()+1)); //在現有行號后追加數據
String fileStr=dataStr.get(0);
String[] fileStrs=fileStr.split(",");
for(int i=0;i<fileStrs.length;i++){
row.createCell(i).setCellValue(fileStrs[i]);
}
out.flush();
wb.write(out);
out.close();
System.out.println(row.getPhysicalNumberOfCells()+" "+row.getLastCellNum());
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void saveReceiveStr(String str){
File file =new File("c:\\zhongan.txt");
if(!file.exists()){
System.out.println("不存在");
try {
System.out.println(file.createNewFile());
file.createNewFile();
} catch (Exception e) {
System.out.println("error");
}
}
BufferedWriter out = null;
//追加信息
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true),"utf-8"));
out.write("時間: "+DateUtils.getNowDate());
out.write("\r\n");
out.write(str);
out.write("\r\n");
out.flush();
System.out.println("寫入成功!");
} catch (Exception e) {
System.out.println("error");
}
}
