package com.b;
import java.io.IOException;
import java.util.ArrayList;
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.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
/**
* 生产Microsoft Excel 工作表及下载
*
* @author Administrator
*
*/
public
class B extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -6607609196117844238L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
queryRecResultDownload(request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void queryRecResultDownload(HttpServletRequest request,
HttpServletResponse response) throws Exception {
List<Object[]> ecProd = new ArrayList<Object[]>();
ecProd.add(new Object[]{"20130711001", "20130711", "621011111100001", "1000.23", "1000.23","20130711", "232", "交易说明1", "失败描述1"});
ecProd.add(new Object[]{"20130711002", "20130711", "621011111100002", "1000.23", "1000.23","20130711", "232", "交易说明2", "失败描述2"});
ecProd.add(new Object[]{"20130711003", "20130711", "621011111100003", "1000.23", "1000.23","20130711", "232", "交易说明3", "失败描述3"});
ecProd.add(new Object[]{"20130711004", "20130711", "621011111100004", "1000.23", "1000.23","20130711", "232", "交易说明4", "失败描述4"});
ecProd.add(new Object[]{"20130711005", "20130711", "621011111100005", "1000.23", "1000.23","20130711", "232", "交易说明5", "失败描述5"});
ecProd.add(new Object[]{"20130711006", "20130711", "621011111100006", "1000.23", "1000.23","20130711", "232", "交易说明6", "失败描述6"});
ecProd.add(new Object[]{"20130711007", "20130711", "621011111100007", "1000.23", "1000.23","20130711", "232", "交易说明7", "失败描述7"});
ecProd.add(new Object[]{"20130711008", "20130711", "621011111100008", "1000.23", "1000.23","20130711", "232", "交易说明8", "失败描述8"});
ecProd.add(new Object[]{"20130711009", "20130711", "621011111100009", "1000.23", "1000.23","20130711", "232", "交易说明9", "失败描述9"});
ecProd.add(new Object[]{"20130711010", "20130711", "621011111100010", "1000.23", "1000.23","20130711", "232", "交易说明10", "失败描述10"});
/** 设置报表类型 */
response = DataExport.setHeadDataResponse(response, "联机交易对账差错报表");
WritableWorkbook workbook = Workbook.createWorkbook(response
.getOutputStream());
/** 设置Sheet */
WritableSheet sheet = workbook.createSheet(" 联机交易对账差错表", 0);
/** 标题 */
WritableCellFormat writableCellFormatTitle = WritableWorkbookUtils
.writableCellFormatTitle();
/** 行数 */
int count = 0;
/** 设置第一行为标题 */
sheet.addCell(new Label(0, count, " 联机交易对账差错报表",
writableCellFormatTitle));
/** 合并单元格 (列、行) */
sheet.mergeCells(0, count, 8, count);
count = count + 2;
/** 设置第三行为标题 */
sheet.addCell(new Label(0, count, " 对账系统:", WritableWorkbookUtils
.writableCellFormatTitleLabelRIGHT()));
/** 设置第三行为标题 */
sheet.addCell(new Label(1, count, " IC卡业务系统", WritableWorkbookUtils
.writableCellFormatTitleLabelLEFT()));
/** 合并单元格 (列、行) */
sheet.mergeCells(1, count, 2, count);
/** 设置第三行为标题 */
sheet.addCell(new Label(6, count, " 交易日期:", WritableWorkbookUtils
.writableCellFormatTitleLabelRIGHT()));
/** 设置第三行为标题 */
sheet.addCell(new Label(7, count, "2013-07-11", WritableWorkbookUtils
.writableCellFormatTitleLabelLEFT()));
/** 合并单元格 (列、行) */
sheet.mergeCells(7, count, 8, count);
if (ecProd != null && ecProd.size() > 0) {
WritableWorkbookUtils.makeListInCardType(sheet, count + 1,
new String[] { "渠道流水", "交易日期", "卡号", "交易金额", "核心对账金额",
"对账日期", "交易码", "交易说明", "失败描述" }, ecProd);
} else {
WritableWorkbookUtils.makeListInCardTypeNull(sheet, count + 1);
}
workbook.write();
workbook.close();
}
}
package com.b;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import jxl.NumberCell;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
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;
public class WritableWorkbookUtils {
/**
* 设置单元格属性(标题)<br>
*
* @return
* @throws WriteException
*/
public static WritableCellFormat writableCellFormatTitle() throws WriteException {
/**
* 标题字体格式 <br>
* 第一个参数:字体类型 <br>
* 第二个参数:字体大小 <br>
* 第三个参数:字体加粗 <br>
* 第四个参数:字体加粗<br>
*
*/
WritableFont font1 = new WritableFont(WritableFont.createFont("宋体"), 12, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE);// NO下划线
/** 设置单元格属性 */
WritableCellFormat format1 = new WritableCellFormat(font1);
// /** 加边框 */
// format1.setBorder(Border.ALL, BorderLineStyle.THIN);
/** 设置居中 */
format1.setAlignment(Alignment.CENTRE);
// format1.setBackground(Colour.GRAY_25);
return format1;
}
public static WritableCellFormat writableCellFormatTitleLabel() throws WriteException {
/**
* 标题字体格式 <br>
* 第一个参数:字体类型 <br>
* 第二个参数:字体大小 <br>
* 第三个参数:字体加粗 <br>
* 第四个参数:字体加粗<br>
*
*/
WritableFont font1 = new WritableFont(WritableFont.createFont("宋体"), 9, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE);// NO下划线
/** 设置单元格属性 */
WritableCellFormat format1 = new WritableCellFormat(font1);
/** 加边框 */
format1.setBorder(Border.ALL, BorderLineStyle.THIN);
/** 设置居中 */
format1.setAlignment(Alignment.CENTRE);
// format1.setBackground(Colour.GRAY_25);
return format1;
}
public static WritableCellFormat writableCellFormatTitleLabelLEFT() throws WriteException {
/**
* 标题字体格式 <br>
* 第一个参数:字体类型 <br>
* 第二个参数:字体大小 <br>
* 第三个参数:字体加粗 <br>
* 第四个参数:字体加粗<br>
*
*/
WritableFont font1 = new WritableFont(WritableFont.createFont("宋体"), 9, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE);// NO下划线
/** 设置单元格属性 */
WritableCellFormat format1 = new WritableCellFormat(font1);
/** 设置居中 */
format1.setAlignment(Alignment.LEFT);
return format1;
}
public static WritableCellFormat writableCellFormatTitleLabelRIGHT() throws WriteException {
/**
* 标题字体格式 <br>
* 第一个参数:字体类型 <br>
* 第二个参数:字体大小 <br>
* 第三个参数:字体加粗 <br>
* 第四个参数:字体加粗<br>
*
*/
WritableFont font1 = new WritableFont(WritableFont.createFont("宋体"), 9, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE);// NO下划线
/** 设置单元格属性 */
WritableCellFormat format1 = new WritableCellFormat(font1);
/** 设置居中 */
format1.setAlignment(Alignment.RIGHT);
return format1;
}
/**
* 设置单元格属性(内容字体格式)
*
* @return
* @throws WriteException
*/
public static WritableCellFormat writableCellFormat() throws WriteException {
/**
* 标题字体格式 <br>
* 第一个参数:字体类型 <br>
* 第二个参数:字体大小 <br>
* 第三个参数:字体加粗 <br>
* 第四个参数:字体加粗<br>
*
*/
/** 内容字体格式 */
WritableFont font2 = new WritableFont(WritableFont.createFont("宋体"), 9, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE);// NO下划线
/** 设置单元格属性 */
WritableCellFormat format1 = new WritableCellFormat(font2);
/** 加边框 */
format1.setBorder(Border.ALL, BorderLineStyle.THIN);
/** 设置居中 */
// format1.setAlignment(Alignment.LEFT);
return format1;
}
public static void makeListInCardType(WritableSheet sheet, int count, String[] fileTitle, List<Object[]> listdata) throws IOException, RowsExceededException, WriteException {
/** 标题Label */
WritableCellFormat writableCellFormatTitleLabel = writableCellFormatTitleLabel();
/** 内容 */
WritableCellFormat writableCellFormat = writableCellFormat();
/**
* 设置列宽度 第一个参数:列数(从0开始) 第二个参数:列宽度
*/
for (int i = 0; i < fileTitle.length; i++) {
sheet.setColumnView(i, 18);
}
/**
* 添加列标题记录<br>
* 第一个参数:列数(从0开始)<br>
* 第二个参数:行数(从0开始) <br>
* 第三个参数:要填充的内容 <br>
* 第四个参数:样式 <br>
*/
for (int j = 0; j < fileTitle.length; j++) {
sheet.addCell(new Label(j, count + 1, fileTitle[j], writableCellFormatTitleLabel));
}
count = count + 2;
for (int k = 0; k < listdata.size(); k++) {
Object[] object = listdata.get(k);
if (object != null && object.length > 0) {
for (int h = 0; h < object.length; h++) {
sheet.addCell(new Label(h, k + count, object[h] != null && !"".equals(object[h]) ? String.valueOf(object[h]).trim() + "" : "", writableCellFormat));
}
}
}
}
public static void makeListInCardTypeNull(WritableSheet sheet,int count) throws IOException, RowsExceededException, WriteException {
WritableFont font1 = new WritableFont(WritableFont.createFont("宋体"), 9, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE);// NO下划线
/** 设置单元格属性 */
WritableCellFormat format1 = new WritableCellFormat(font1);
/** 设置居中 */
format1.setAlignment(Alignment.CENTRE);
/** 设置第一行为标题 */
sheet.addCell(new Label(0, count, " 无数据。。。", format1));
/** 合并单元格 (列、行) */
sheet.mergeCells(0, count, 8, count);
}
public static void makeListInCardType(OutputStream os, String[] fileTitle, List<Object[]> listdata, String Title) throws IOException, RowsExceededException, WriteException {
String fPath = "D:\\联机交易对账差错表.xls";
File file = new File(fPath);
// wb = Workbook.createWorkbook(file);
WritableWorkbook workbook = Workbook.createWorkbook(file);
/** 设置Sheet */
WritableSheet sheet = workbook.createSheet(" 联机交易对账差错表", 0);
/** 标题 */
WritableCellFormat writableCellFormatTitle = writableCellFormatTitle();
/** 标题Label */
WritableCellFormat writableCellFormatTitleLabel = writableCellFormatTitleLabel();
/** 内容 */
WritableCellFormat writableCellFormat = writableCellFormat();
/**
* 设置列宽度 第一个参数:列数(从0开始) 第二个参数:列宽度
*/
for (int i = 0; i < fileTitle.length; i++) {
sheet.setColumnView(i, 18);
}
/** 行数 */
int count = 0;
/** 设置第一行为标题 */
sheet.addCell(new Label(0, count, " 联机交易对账差错报表", writableCellFormatTitle));
/** 合并单元格 (列、行) */
sheet.mergeCells(0, count, 8, count);
/**
* 添加列标题记录<br>
* 第一个参数:列数(从0开始)<br>
* 第二个参数:行数(从0开始) <br>
* 第三个参数:要填充的内容 <br>
* 第四个参数:样式 <br>
*/
for (int j = 0; j < fileTitle.length; j++) {
sheet.addCell(new Label(j, count + 1, fileTitle[j], writableCellFormatTitleLabel));
}
count = count + 2;
for (int k = 0; k < listdata.size(); k++) {
Object[] object = listdata.get(k);
if (object != null && object.length > 0) {
for (int h = 0; h < object.length; h++) {
// sheet.addCell(new jxl.write.Number(h, k + count,2.33,writableCellFormat));
sheet.addCell(new Label(h, k + count, object[h] != null && !"".equals(object[h]) ? String.valueOf(object[h]).trim() + "" : "", writableCellFormat));
}
}
}
workbook.write();
workbook.close();
}
public static void makeListInCardTypeNew(WritableSheet sheet, int count, String[] fileTitle, List<Object[]> listdata) throws IOException, RowsExceededException, WriteException {
/** 标题Label */
WritableCellFormat writableCellFormatTitleLabel = writableCellFormatTitleLabel();
/** 内容 */
WritableCellFormat writableCellFormatNew = writableCellFormatNew();
/**
* 设置列宽度 第一个参数:列数(从0开始) 第二个参数:列宽度
*/
for (int i = 0; i < fileTitle.length; i++) {
sheet.setColumnView(i, 18);
}
/**
* 添加列标题记录<br>
* 第一个参数:列数(从0开始)<br>
* 第二个参数:行数(从0开始) <br>
* 第三个参数:要填充的内容 <br>
* 第四个参数:样式 <br>
*/
for (int j = 0; j < fileTitle.length; j++) {
sheet.addCell(new Label(j, count, fileTitle[j], writableCellFormatTitleLabel));
}
count = count + 1;
for (int k = 0; k < listdata.size(); k++) {
Object[] object = listdata.get(k);
if (object != null && object.length > 0) {
for (int h = 0; h < object.length; h++) {
sheet.addCell(new Label(h, k + count, object[h] != null && !"".equals(object[h]) ? String.valueOf(object[h]).trim() + "" : "", writableCellFormatNew));
}
}
}
}
/**
* 设置单元格属性(内容字体格式)
*
* @return
* @throws WriteException
*/
public static WritableCellFormat writableCellFormatNew() throws WriteException {
/**
* 标题字体格式 <br>
* 第一个参数:字体类型 <br>
* 第二个参数:字体大小 <br>
* 第三个参数:字体加粗 <br>
* 第四个参数:字体加粗<br>
*
*/
/** 内容字体格式 */
WritableFont font2 = new WritableFont(WritableFont.createFont("宋体"), 9, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE);// NO下划线
/** 设置单元格属性 */
WritableCellFormat format1 = new WritableCellFormat(font2);
/** 加边框 */
format1.setBorder(Border.ALL, BorderLineStyle.THIN);
/** 设置居中 */
format1.setAlignment(Alignment.CENTRE);
return format1;
}
/**
* 设置单元格属性(标题)<br>
*
* @return
* @throws WriteException
*/
public static WritableCellFormat writableCellFormatTitleNew() throws WriteException {
/**
* 标题字体格式 <br>
* 第一个参数:字体类型 <br>
* 第二个参数:字体大小 <br>
* 第三个参数:字体加粗 <br>
* 第四个参数:字体加粗<br>
*
*/
WritableFont font1 = new WritableFont(WritableFont.createFont("宋体"), 15, WritableFont.BOLD, false, UnderlineStyle.SINGLE);// NO下划线
/** 设置单元格属性 */
WritableCellFormat format1 = new WritableCellFormat(font1);
// /** 加边框 */
// format1.setBorder(Border.ALL, BorderLineStyle.THIN);
/** 设置居中 */
format1.setAlignment(Alignment.CENTRE);
// format1.setBackground(Colour.GRAY_25);
return format1;
}
public static WritableCellFormat writableCellFormatTitleLabelNew() throws WriteException {
/**
* 标题字体格式 <br>
* 第一个参数:字体类型 <br>
* 第二个参数:字体大小 <br>
* 第三个参数:字体加粗 <br>
* 第四个参数:字体加粗<br>
*
*/
WritableFont font1 = new WritableFont(WritableFont.createFont("宋体"), 12, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE);// NO下划线
/** 设置单元格属性 */
WritableCellFormat format1 = new WritableCellFormat(font1);
/** 加边框 */
// format1.setBorder(Border.ALL, BorderLineStyle.THIN);
/** 设置居中 */
format1.setAlignment(Alignment.LEFT);
// format1.setBackground(Colour.GRAY_25);
return format1;
}
}