懒得看那些使用专用包来保存查询数据存成excel的程序了,尝试用最原始、个人感觉比较简单的方式实现这个功能,下面是servlet方法的主要内容部分,演示了该方法的实现思路和关键程序,留做记录。
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/vnd.ms-excel;charset=gbk");//第一个设定响应包类型,第二个放至excel中文乱码
response.setHeader("Content-Disposition", "attachment; filename=outExcelFromDB.xls");
PrintWriter out = response.getWriter();
String[][] str =new String[4][5];//实际应用直接从其他途径获得二维字符串数组即可;
for(int i=0;i<4;i++){
for(int j=0;j<5;j++){
str[i][j]="值"+String.valueOf(i*10+j);
}
}
String inStr ="公司\t第一季度\t第一季度\t第一季度\t第一季度\t总计";
for(int i=0;i<4;i++){
inStr+="\r\n哈哈";
for(int j=0;j<5;j++){
inStr+="\t"+str[i][j];
}
}
out.println(inStr);
}