新做一个程序,需要打印学生的准考证,思路是先导出到Excel中,然后打印:
package com.zhaosoft.servlet;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jxl.Workbook;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import com.zhaosoft.bean.Students;
import com.zhaosoft.bean.StudentsDAO;
import com.zhaosoft.util.Excel;
public class ExportExcel extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String imagepath = "";
List list = (List) request.getSession().getAttribute("list");
Students student = null;
// 对数据集进行遍历
WritableWorkbook wwb = null;
try {
// 首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象
String path = request.getRealPath("") + "/excel";
wwb = Workbook.createWorkbook(new File(path + "/1.xls"));
} catch (IOException e) {
e.printStackTrace();
}
if (wwb != null) {
// 创建一个可写入的工作表
// Workbook的createSheet方法有两个参数,第一个是工作表的名称,第二个是工作表在工作薄中的位置
WritableSheet ws = wwb.createSheet("sheet1", 0);
// 设置列的宽度
ws.setColumnView(0, 10);
ws.setColumnView(1, 27);
ws.setColumnView(2, 17);
ws.setColumnView(3, 3);
WritableCellFormat ccf = new WritableCellFormat(new WritableFont(
WritableFont.ARIAL, 12, WritableFont.BOLD, false));
WritableCellFormat Bleft = new WritableCellFormat();
WritableCellFormat Bright = new WritableCellFormat();
WritableCellFormat Bbottom = new WritableCellFormat();
WritableCellFormat BLeftAndRight = new WritableCellFormat(
new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD,
false));
try {
BLeftAndRight.setBorder(Border.LEFT, BorderLineStyle.THIN,
Colour.BLACK);
BLeftAndRight.setBorder(Border.RIGHT, BorderLineStyle.THIN,
Colour.BLACK);
BLeftAndRight.setAlignment(jxl.format.Alignment.CENTRE);
Bright.setBorder(Border.RIGHT, BorderLineStyle.THIN,
Colour.BLACK);
Bbottom.setBorder(Border.BOTTOM, BorderLineStyle.THIN,
Colour.BLACK);
Bbottom.setBorder(Border.LEFT, BorderLineStyle.THIN,
Colour.BLACK);
Bbottom.setBorder(Border.RIGHT, BorderLineStyle.THIN,
Colour.BLACK);
Bleft
.setBorder(Border.LEFT, BorderLineStyle.THIN,
Colour.BLACK);
Bleft.setAlignment(jxl.format.Alignment.LEFT);
// 把水平对齐方式指定为居中
ccf.setAlignment(jxl.format.Alignment.CENTRE);
// 把垂直对齐方式指定为居中
ccf.setVerticalAlignment(jxl.format.VerticalAlignment.BOTTOM);
ccf.setBorder(Border.TOP, BorderLineStyle.THIN, Colour.BLACK);
ccf.setBorder(Border.LEFT, BorderLineStyle.THIN, Colour.BLACK);
ccf.setBorder(Border.RIGHT, BorderLineStyle.THIN, Colour.BLACK);
// 设置自动换行
// ccf.setWrap(true);
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < list.size(); i++) {
student = (Students) list.get(i);
imagepath = request.getRealPath("") + "/images/"
+ student.getZp();
try {
// 合并单元格
ws.mergeCells(0, (i * 11) + 0, 3, (i * 11) + 0);
ws.mergeCells(0, (i * 11) + 1, 3, (i * 11) + 1);
ws.mergeCells(2, (i * 11) + 2, 3, (i * 11) + 8);
ws.mergeCells(0, (i * 11) + 9, 3, (i * 11) + 9);
for (int j = 0; j < 10; j++) {
ws.setRowView((i * 11) + j, 400);
}
ws.setRowView((i * 11) + 9, 200);
ws.setRowView((i * 11) + 10, 200);
} catch (Exception e) {
e.printStackTrace();
}
// 这里需要注意的是,在Excel中,第一个参数表示列,第二个表示行
Label Title1 = new Label(0, (i * 11) + 0, "华北科技学院新维计算机专修学校",
ccf);
Label Title2 = new Label(0, (i * 11) + 1, "入学考试主考证",
BLeftAndRight);
Label name1 = new Label(0, (i * 11) + 2, "姓 名:", Bleft);
Label name2 = new Label(1, (i * 11) + 2, student.getName());
Label zkzh1 = new Label(0, (i * 11) + 3, "准考证号:", Bleft);
Label zkzh2 = new Label(1, (i * 11) + 3, student.getZkzh());
Label sfzh1 = new Label(0, (i * 11) + 4, "身份证号:", Bleft);
Label sfzh2 = new Label(1, (i * 11) + 4, student.getSfz());
Label ksdd1 = new Label(0, (i * 11) + 5, "考试地点:", Bleft);
Label ksdd2 = new Label(1, (i * 11) + 5, student.getKsdd());
Label kch1 = new Label(0, (i * 11) + 6, "考 场 号:", Bleft);
Label kch2 = new Label(1, (i * 11) + 6, student.getKch());
Label zwh1 = new Label(0, (i * 11) + 7, "座 位 号:", Bleft);
Label zwh2 = new Label(1, (i * 11) + 7, student.getZwh());
Label kssj1 = new Label(0, (i * 11) + 8, "考试时间:", Bleft);
Label kssj2 = new Label(1, (i * 11) + 8, student.getKsrq());
Label tp = new Label(2, (i * 11) + 2, "", Bright);
Label bottom = new Label(0, (i * 11) + 9, "", Bbottom);
try {
// 将生成的单元格添加到工作表中
ws.addCell(Title1);
ws.addCell(Title2);
ws.addCell(name1);
ws.addCell(name2);
ws.addCell(zkzh1);
ws.addCell(zkzh2);
ws.addCell(sfzh1);
ws.addCell(sfzh2);
ws.addCell(ksdd1);
ws.addCell(ksdd2);
ws.addCell(kch1);
ws.addCell(kch2);
ws.addCell(zwh1);
ws.addCell(zwh2);
ws.addCell(kssj1);
ws.addCell(kssj2);
ws.addCell(tp);
ws.addCell(bottom);
File f = new File(imagepath);
if (f.isFile()) {
new Excel().insertImg(ws, 2, (i * 11) + 2, 1, 6, f);
}
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
try {
// 从内存中写入文件中
wwb.write();
// 关闭资源,释放内存
wwb.close();
} catch (IOException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
response.sendRedirect("excel/1.xls");
//response.getWriter().write("<script language='javascript'> window.location='excel/1.xls';</script>");
}
}