Springmvc框架下使用jxl方法把表格导出成excel简单方法 步骤: 1、导入jxl包
D:\\dev\\repository\\net\\sourceforge\\jexcelapi\\jxl\\2.6.12\\jxl-2.6.12.jar 添加依赖:
import java.io.File;
import java.io.IOException; import java.io.OutputStream;
import java.text.SimpleDateFormat; import java.util.List; import jxl.write.Label;
import jxl.write.WritableCellFormat; import jxl.write.WritableFont; import jxl.write.WritableImage;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import com.mvr.storeManagement.model.StockOut;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.VerticalAlignment; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook;
public class ExcelOutService {
public void downloadExcel(HttpServletResponse response, List
SimpleDateFormatsdf =new SimpleDateFormat(\SimpleDateFormatsd =new SimpleDateFormat(\WritableWorkbook book = null;
response.reset();
response.setCharacterEncoding(\OutputStreamos = null; try {
response.setContentType(\
response.setCharacterEncoding(\
response.setHeader(\设置生成的文件名字
os = response.getOutputStream();
book = Workbook.createWorkbook(os); } catch (Exception e) {
e.printStackTrace(); } try {
WritableSheet sheet = book.createSheet(\出库单\
//wcff代表设置字体 加粗 字体大小 局中 WritableFontwfs = new
WritableFont(WritableFont.TIMES,15,WritableFont.BOLD,false);//设置字体
WritableCellFormatwcff=new WritableCellFormat(wfs); wcff.setAlignment(Alignment.CENTRE);//居中对齐
//wcff1代表 设置字体大小 加粗 剧中 不加边框 WritableFont wfs2 = new
WritableFont(WritableFont.TIMES,10,WritableFont.BOLD,false);//设置字体
WritableCellFormat wcff1=new WritableCellFormat(wfs2);
wcff1.setVerticalAlignment(VerticalAlignment.CENTRE);//设置垂直剧中
//wcff3代表 字体加粗 加边框 WritableFont wfs4 = new
WritableFont(WritableFont.TIMES,10,WritableFont.BOLD,false);//设置字体
WritableCellFormat wcff3=new WritableCellFormat(wfs4);
wcff3.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);//设置边框
//wcff2代表 设置字体 不加粗 剧中 加边框
WritableFont wfs3 = new
WritableFont(WritableFont.TIMES,10,WritableFont.NO_BOLD,false);//设置字体
WritableCellFormat wcff2=new WritableCellFormat(wfs3);
wcff2.setVerticalAlignment(VerticalAlignment.CENTRE);//设置垂直剧中
wcff2.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);//设置边框
String
picpath=request.getSession().getServletContext().getRealPath(\\\\\image\ File file = new File(picpath);
// File file = new File(\
WritableImage image = new WritableImage(0, 0, 2, 2, file); sheet.addImage(image);
sheet.setColumnView(1, 20);//设置第二列 sheet.setColumnView(3, 20); sheet.setRowView(0, 600); sheet.setRowView(1, 400);
sheet.mergeCells(1, 0, 6, 0);// 合并第三列列第一行到第六列第一行的所有单元格
sheet.mergeCells(1, 1, 6, 1);//合并第3列第二行到第六列第二行 sheet.mergeCells(1, 2, 3, 2);//合并第二列第三行到第4列第三行 sheet.mergeCells(1, 3, 2, 3);//合并第二列第四行到第三列第四行 sheet.mergeCells(4, 3, 6, 3); sheet.mergeCells(0, 0, 1, 1);
Label lable= new Label(1,0,\天津科技有限公司\ Label lable1= new Label(1,1,\领用出库单\ Label lable2= new Label(0,2,\单号\
Label lable7= new
Label(1,2,\-00\
Label lable3= new Label(4,2,\库房一\ Label lable4= new Label(5,2,\制单日期\
Label lable10= new
Label(6,2,sdf.format(list.get(0).getStock_out_time()),wcff1); Label lable5= new Label(0,3,\出库类型\ Label lable8= new Label(1,3,\生产出库\ Label lable6= new Label(3,3,\项目单号\// Label lable9= new Label(4,3,\北京兴业\ Label lable13= new Label(0,4,\设备代码:\sheet.mergeCells(0, 4, 2,4);
Label lable14= new Label(3,4,\产品编号:\sheet.mergeCells(3, 4, 6,4);
Label lable15= new Label(0,5,\组装人:\sheet.mergeCells(0, 5, 2,5);
Label lable16= new Label(3,5,\质检人:\sheet.mergeCells(3, 5, 6,5);
sheet.addCell(lable); sheet.addCell(lable1); sheet.addCell(lable2); sheet.addCell(lable3); sheet.addCell(lable4); sheet.addCell(lable5); sheet.addCell(lable6); sheet.addCell(lable7); sheet.addCell(lable8); // sheet.addCell(lable9); sheet.addCell(lable10);
sheet.addCell(new jxl.write.Label(0, 6, \序号\第一列 sheet.addCell(new jxl.write.Label(1, 6, \物料编码\
sheet.addCell(new jxl.write.Label(2,6, \货品名称\sheet.addCell(new jxl.write.Label(3, 6, \规格型号\sheet.addCell(new jxl.write.Label(4, 6, \单位\sheet.addCell(new jxl.write.Label(5, 6, \数量\sheet.addCell(new jxl.write.Label(6, 6, \备注\
for (inti = 7; i sheet.addCell(new jxl.write.Label(0, i, String.valueOf(i-6),wcff2));// 序号从1开始 sheet.addCell(new jxl.write.Label(1, i, list.get(i - 7).getMaterial_code(),wcff2)); sheet.addCell(new jxl.write.Label(2, i, list.get(i - 7).getProduct_nm(),wcff2)); sheet.addCell(new jxl.write.Label(3, i, list.get(i - 7).getNorms(),wcff2)); sheet.addCell(new jxl.write.Label(4, i, list.get(i - 7).getUnit(),wcff2)); sheet.addCell(new jxl.write.Label(5, i, list.get(i - 7).getStock_out_count().toString(),wcff2)); sheet.addCell(new jxl.write.Label(6, i, list.get(i - 7).getDetails(),wcff2)); } } } 3、控制层 Label lable11= new Label(1,list.size()+7,\领料人:\ Label lable12= new Label(3,list.size()+7,\库管员:\ Label lable17= new Label(5,list.size()+7,\审批人:\sheet.addCell(lable17); sheet.addCell(lable11); sheet.addCell(lable12); sheet.addCell(lable13); sheet.addCell(lable14); sheet.addCell(lable15); sheet.addCell(lable16); book.write(); book.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { os.flush(); os.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } request.getSession().setAttribute(\stockOutDao.insertStockOut(soList); List ExcelOutService service=new ExcelOutService(); service.downloadExcel(response, rtList,request); 百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库jxl-jsp页面表格导出excel在线全文阅读。
相关推荐: