public static void mikile() {
final String[] header = new String[] { "name", "sex", "age" };
final String[][] data = new String[][] { header, { "日日", "F", "22" },
{ "Tom", "M", "25" }, { "Lily", "F", "19" } };
File tempFile = new File("C://lixin.csv");
try {
CSVWriter writer = new CSVWriter(new FileWriter(tempFile));
for (int i = 0; i < data.length; i++) {
writer.writeNext(data[i]);
}
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//导出excel文件
public static void downloadFile(HttpServletResponse response,
String fullfileName) throws Exception {
// String filePath2 = context.getRequest().getRealPath("/");
// fullfileName是已经生成excel文件(存在数据的)的路径名
// 例如:e://file/template/454566412221.xls
if (fullfileName == null || fullfileName.equals(""))
return;
File file = null;
try {
file = new File(fullfileName);
FileInputStream fileInputStream = new FileInputStream(file);
DataInputStream dataInputStream = new DataInputStream(
fileInputStream);
byte[] downContext = new byte[dataInputStream.available()];
dataInputStream.read(downContext);
fileInputStream.close();
dataInputStream.close();
response.reset();
response.setContentType("application/octet-stream;charset=UTF-8");// 设置内容类型
response.setHeader("Content-Length", String.valueOf(file.length()));// 获取文件的大小
response.setHeader("Content-Disposition","attachment;filename=" +"lixin.csv");//显示导出之后的文件名,调用下面的方法
OutputStream outputStream = response.getOutputStream();// 导出文件
outputStream.write(downContext);// 将数据导出到客户端内存中
outputStream.flush();
outputStream.close();
} catch (Exception e) {
throw new Exception(e.getMessage());
} finally {
if (file.exists()) {
//file.delete();
}
}
}
需添加opencsv-2[1].0.jar