日期:2014-05-16 浏览次数:20704 次
/**
* List导出为csv格式
* @param arrayList
* @param file 已经创建好的file文件
* @throws Exception
*/
private void exportFile(List<String[]> arrayList,File file) throws Exception{
OutputStream out = new FileOutputStream(file,true);
for(String [] arr:arrayList){
for(int i = 0 ; i < arr.length ; i ++){
out.write(arr[i]==null?"".getBytes():arr[i].getBytes());
if(i<arr.length-1)out.write(",".getBytes());
}
out.write('\r'); // \r\n表示换行
out.write('\n');
}
out.close();
}
public static void ExportCsv(String fileName,List<String[]> list,HttpServletResponse response){
PrintWriter out = null;
CSVWriter writer = null;
List<String[]> lines = new ArrayList<String[]>();
lines.addAll(list);
try{
response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName.getBytes("gb2312"), "iso8859-1") + ".csv" + "\"");
response.setCharacterEncoding("Gb2312");
response.setContentType("text/plain;charset=Gb2312");
out = response.getWriter();
writer = new CSVWriter(out);
writer.writeAll(lines);
writer.flush();
}catch (IOException e){
error_logger.error("CsvExport.ExportCsv() error!",e);
} catch (Exception e){
error_logger.error("CsvExport.ExportCsv() error!",e);
}finally{
try{
writer.close();
out.close();
}catch(Exception ex){}
}
}
/*
* 一键备份指定表
* @deprecated
* @return
*/
public String backupTable_old(){
//存放备份文件的路径
String backup_path = getProps("backup_file_path");
//配置文件中获取数据库连接信息
String user_name = getProps("user_name");
String user_psw = getProps("user_psw");
String db_name = getProps("db_name");
String host_ip = getProps("host_ip");
//获取文件保存备份路径,如果不存在,则先创建目录
String saveFileDir=backup_path + ab + "/";
File dir=new File(saveFileDir);
if(!dir.exists())dir.mkdirs();
backup_path = saveFileDir + "backup_"+ab+"_"+dataFormat.format(new Date())+".sql";
//需要备份的表,目前为已定义好的表
String tableClass = TB_CLASS + ab;
String tableClassUser = TB_CLASS_USER + ab;
String black = " ";
String allTable = black + tableClass + black + tableClassUser;
StringBuffer sb = new StringBuffer();
//拼接字符串,mysqldump命令。
sb.append("mysqldump").append(" -h ").append(host_ip).append(" -u ").append(user_name)
.append(" -p").append(user_psw).append(black).append(db_name).append(" --add-drop-table")
.append(" -t ").append(allTable).append(" --result-file=").append(backup_path);
String dumpSQL = sb.toString();
System.out.println(dumpS