导出excel
程序员文章站
2024-03-20 14:03:34
...
package geekcom.lionsgdadmin.survey.action;
import geekcom.lionsgdadmin.survey.manager.ISurveyManager;
import geekcom.lionsgdadmin.survey.model.Survey_type;
import geekcom.lionsgdadmin.survey.model.formbean.Survey_typeFormbean;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
public class IsExcel extends SimpleFormController {
private ISurveyManager manager;
public ISurveyManager getManager() {
return manager;
}
public void setManager(ISurveyManager manager) {
this.manager = manager;
}
@Override
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
Survey_typeFormbean form = (Survey_typeFormbean) command;
String name = form.getName().trim();
int toPage = request.getParameter("toPage").equals("") ? 1 : Integer
.parseInt(request.getParameter("toPage"));
int pageSize = 10;
//查询出来的结果 ,即需要导出的数据
List<Survey_type> list = manager.getSurvey_type(toPage, pageSize, name);
StringBuffer sbFileName = new StringBuffer();
sbFileName.append("问卷类型维护报表.xls");
String fileName = "";
/*
* 浏览器不同 可能会造成导出的excel文件名出现乱码,解决方案是判断浏览器
* 然后根据不同的浏览器返回不同的编码格式.
*/
if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {
fileName = new String(sbFileName.toString().getBytes("utf-8"),
"iso-8859-1");
} else {
fileName = URLEncoder.encode(sbFileName.toString(), "UTF-8");
}
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-excel");
response.setHeader("Content-Disposition", "attachment; filename="
+ fileName);
OutputStream os = response.getOutputStream();
exportToExcel(list, os);
return null;
}
@Override
protected Object formBackingObject(HttpServletRequest request)
throws Exception {
return new Survey_typeFormbean();
}
@SuppressWarnings( { "unused", "unchecked" })
private static void exportToExcel(List list, OutputStream os)
throws Exception {
WritableWorkbook workbook = Workbook.createWorkbook(os);
WritableSheet sheet1 = workbook.createSheet("报表", 0);
sheet1.addCell(new Label(0, 0, "类别标题"));
sheet1.addCell(new Label(1, 0, "创建人"));
sheet1.addCell(new Label(2, 0, "创建时间"));
for (int i = 0, n = list.size(); i < n; i++) {
Survey_type info = (Survey_type) list.get(i);
Label name = new Label(0, i + 1, info.getName());
sheet1.addCell(name);
Label create_oper = new Label(1, i + 1, info.getCreate_oper());
sheet1.addCell(create_oper);
Label create_time = new Label(2, i + 1, info.getCreate_time());
sheet1.addCell(create_time);
}
workbook.write();
workbook.close();
}
}
上一篇: Servlet Cookie
推荐阅读
-
导出excel
-
java 使用EasyExcel导出Excel
-
掌财社:Java如何使用EasyExcel实现导入导出excel
-
java导出excel表格 使用alibaba easyexcel
-
EasyExcel使用ResponseEntity导出excel
-
java使用阿里的easyExcel实现Excel导出功能
-
Ireport,JAVA报表制作并导出(pdf、excel、word) 博客分类: IreportJAVA报表制作 ireport
-
birt 3.7.1导出PDF报表中文问题 博客分类: eclipse birt3.7.1PDF中文乱码
-
eclipse中POI操作EXCEL简述 博客分类: exceleclipse eclipse excelPOI
-
PHP导出Excel 博客分类: PHP phpexcel