当前位置:编程学习 > JAVA >>

各位大神,求帮忙????

小弟求一个将从数据库查询出来的数据集合导出到execl的代码,希望在点击弹出时有下载框提示。谢谢各位了。。。 --------------------编程问答-------------------- response.setHeader("Content-disposition","attachment;filename=" + URLEncoder.encode(你的文件名, "utf-8")); --------------------编程问答-------------------- js window.open 导出excel 的url。 --------------------编程问答-------------------- 在jsp页面上使用了如下的代码: 
response.setHeader("Content-disposition", "attachment; filename=print_tmp.xls");
当浏览器访问页面的时候。就会给出可以下载excel的提示框。 --------------------编程问答-------------------- 不行呀,我是用jxl写的

public class ExeclPortServlet extends HttpServlet {

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Override
public void init() throws ServletException {
super.init();

ServletContext servletContext = this.getServletContext();

WebApplicationContext ctx = WebApplicationContextUtils
.getWebApplicationContext(servletContext);

hibernateTemplate = (HibernateTemplate) ctx
.getBean("hibernateTemplate");
}

HibernateTemplate hibernateTemplate;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);

}

@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

try {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
//PrintWriter out = response.getWriter();
String userid = request.getParameter("userid");
List<Users> list = new ArrayList<Users>();
List<Users> list1 = null;
Users user = new Users();
String[] arr;
arr = userid.split(",");
for (int i = 0; i < arr.length; i++) {
if (arr[i] != "") {
try {
list1 = this.hibernateTemplate
.find("from Users where userid='" + arr[i] + "'");
for (Users users : list1) {
user = users;
}
} catch (Exception e) {
e.printStackTrace();
}
}
list.add(user);
}
UsersExportExcel uee = new UsersExportExcel();
String path = getServletContext().getRealPath("/") + "excel";
File temp = new File(path);
if (!temp.exists()) {
temp.mkdirs();
}
path = path + File.separator + "用户信息表.xls";
FileOutputStream outfile = new FileOutputStream(path);

uee.export(list, outfile);    //调用导出

File file = new File(path);
BufferedInputStream inputStream = new BufferedInputStream(
new FileInputStream(file));
response.reset();
response.setContentType("application/x-msdownload");
response.addHeader("Content-Disposition",
"attachment;filename=ExamArrange.xls");
byte[] by = new byte[2048];
   int len = 0;
       while((len=(inputStream.read(by)))>0){
   response.getOutputStream().write(by,0,len);
   //out.clear();
   //out=pageContext.pushBody();
   }
   inputStream.close();
   if(file.isFile() && file.exists()){
file.delete();
   }
} catch (Exception e) {
e.printStackTrace();
}

}



public class UsersExportExcel {
public static void export(List<Users> list,FileOutputStream os) throws Exception{

jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(os);
WritableSheet ws = wwb.createSheet("用户信息表", 0);
jxl.write.Label labelC;

WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16,
WritableFont.BOLD, false,
jxl.format.UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK); 

 // excel 表头
        labelC = new jxl.write.Label(0, 0, "序号");
        ws.addCell(labelC); // 写入excel
        labelC = new jxl.write.Label(1, 0, "用户名");
        ws.addCell(labelC);
        labelC = new jxl.write.Label(2, 0, "密码");
        ws.addCell(labelC);
        labelC = new jxl.write.Label(3, 0, "姓名");
        ws.addCell(labelC);
        labelC = new jxl.write.Label(4, 0, "性别");
        ws.addCell(labelC);
        labelC = new jxl.write.Label(5, 0, "是否付费");
        ws.addCell(labelC);
        labelC = new jxl.write.Label(6, 0, "邮箱");
        ws.addCell(labelC);
        labelC = new jxl.write.Label(7, 0, "出生年月");
        ws.addCell(labelC);
        labelC = new jxl.write.Label(8, 0, "出生地");
        ws.addCell(labelC);
        labelC = new jxl.write.Label(9, 0, "居住地");
        ws.addCell(labelC);
        labelC = new jxl.write.Label(10, 0, "学历");
        ws.addCell(labelC);
        labelC = new jxl.write.Label(11, 0, "职业状态");
        ws.addCell(labelC);
        labelC = new jxl.write.Label(12, 0, "职业");
        ws.addCell(labelC);
        labelC = new jxl.write.Label(13, 0, "行业");
        ws.addCell(labelC);
        labelC = new jxl.write.Label(14, 0, "毕业院校");
        ws.addCell(labelC);
        labelC = new jxl.write.Label(15, 0, "就业年份");
        ws.addCell(labelC);
        labelC = new jxl.write.Label(16, 0, "专业");
        ws.addCell(labelC);
        labelC = new jxl.write.Label(17, 0, "报告类型");
        ws.addCell(labelC);
        labelC = new jxl.write.Label(18, 0, "登录时间");
        ws.addCell(labelC);
        labelC = new jxl.write.Label(19, 0, "毕业院校");
        ws.addCell(labelC);
        labelC = new jxl.write.Label(20, 0, "测试时间");
        ws.addCell(labelC);
        labelC = new jxl.write.Label(21, 0, "测试结果");
        ws.addCell(labelC);
        labelC = new jxl.write.Label(22, 0, "用户类型");
        ws.addCell(labelC);
        labelC = new jxl.write.Label(23, 0, "注册时间");
        ws.addCell(labelC);
        
        int i = 1; // 定义 excel 行号,从第二行开始。
        int j = 0; // 定义 excel 列号
        
        for (Users users : list) {
         // 第一列
            labelC = new jxl.write.Label(j, i, users.getUserid()+"");
            ws.addCell(labelC);
 
            // 第二列
            j += 1;
            labelC = new jxl.write.Label(j, i, users.getLogin_name());
            ws.addCell(labelC);
 
            // 第三列
            j += 1;
            labelC = new jxl.write.Label(j, i,users.getLogin_pass());
            ws.addCell(labelC);
            
            j += 1;
            labelC = new jxl.write.Label(j, i,users.getUsername());
            ws.addCell(labelC);
            
            j += 1;
            labelC = new jxl.write.Label(j, i,users.getSex());
            ws.addCell(labelC);
            
            j += 1;
            labelC = new jxl.write.Label(j, i,users.getIsfree()+"");
            ws.addCell(labelC);
            
            j += 1;
            labelC = new jxl.write.Label(j, i,users.getEmail());
            ws.addCell(labelC);
            
            j += 1;
            //jxl.write.DateTime labelc = new jxl.write.DateTime(j,i,users.getBirthday());  
            labelC = new jxl.write.Label(j, i,users.getBirthday()+"");
            ws.addCell(labelC);
            
            j += 1;
            labelC = new jxl.write.Label(j, i,users.getBirth_address());
            ws.addCell(labelC);
            
            j += 1;
            labelC = new jxl.write.Label(j, i,users.getAddress());
            ws.addCell(labelC);
            
            j += 1;
            labelC = new jxl.write.Label(j, i,users.getDegree());
            ws.addCell(labelC);
            
            j += 1;
            labelC = new jxl.write.Label(j, i,users.getProfession_state());
            ws.addCell(labelC);
            
            j += 1;
            labelC = new jxl.write.Label(j, i,users.getProfession());
            ws.addCell(labelC);
            
            j += 1;
            labelC = new jxl.write.Label(j, i,users.getSector());
            ws.addCell(labelC);
            
            j += 1;
            labelC = new jxl.write.Label(j, i,users.getCollege());
            ws.addCell(labelC);
            
            j += 1;
            labelC = new jxl.write.Label(j, i,users.getJob_date()+"");
            ws.addCell(labelC);
            
            j += 1;
            labelC = new jxl.write.Label(j, i,users.getSubject());
            ws.addCell(labelC);
            
            j += 1;
            labelC = new jxl.write.Label(j, i,users.getType_report());
            ws.addCell(labelC);
            
            j += 1;
            labelC = new jxl.write.Label(j, i,users.getLogin_time()+"");
            ws.addCell(labelC);
            
            j += 1;
            labelC = new jxl.write.Label(j, i,users.getCollege());
            ws.addCell(labelC);
            
            j += 1;
            labelC = new jxl.write.Label(j, i,users.getTest_time()+"");
            ws.addCell(labelC);
            
            j += 1;
            labelC = new jxl.write.Label(j, i,users.getScore());
            ws.addCell(labelC);
            
            j += 1;
            labelC = new jxl.write.Label(j, i,users.getUserType());
            ws.addCell(labelC);
            
            j += 1;
            labelC = new jxl.write.Label(j, i,users.getRegister_date()+"");
            ws.addCell(labelC);
        
            // 下一行
            i += 1;
            // 重置为第一列
            j = 0;
}
        wwb.write();
        // 关闭Excel工作薄对象
        wwb.close();
}
}

能够导出,但是不弹出下载框提示,求解答??
--------------------编程问答--------------------   跟浏览器有关吧,,,有的是直接下载了,有的是要保持路径!!  在不行 就自己写个 下载 把服务器生成的xml 文件 下载 到本地 --------------------编程问答-------------------- excel 文件 --------------------编程问答-------------------- if(confirm('楼主确定要下载?')){

} --------------------编程问答-------------------- 除
补充:Java ,  Java EE
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,