📄 guesttable.java
字号:
package view.mainframe.guestconsumed.guestcheckoutframe;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.Vector;
import dao.consumeddao.GoodsConsumedDao;
import vo.GoodsConsumedInfo;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
public class GuestTable {
public void writeExcelOfFile(String fileName, Vector vectorData,
Vector vectorTitle) {
WritableWorkbook excelModel = null; // Excle文件写出操作对象
try {
// 创建Excel文件写出操作对象
File file = new File(fileName);
excelModel = Workbook.createWorkbook(file);
// 调用生成Excel列表方法设置并生成Excel
this.writeExcelFile(excelModel, vectorData);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
excelModel.close(); // 必须关闭创建Excel文档的对象
} catch (WriteException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 写出Excel文件
*
* @param excelModel
* @param argTableVO
* @throws WriteOutExcelException
*/
private void writeExcelFile(WritableWorkbook excelModel,
Vector argVectorData) throws Exception {
try {
/* 设置标题 */
String title = "宾客消费信息详细查询";
// 设置页面Sheet
WritableSheet ws = excelModel.createSheet(title, 0);
// 设置头
this.buildExlHead(ws, title);
// 每一个单元个的样式
WritableCellFormat callFormat = new WritableCellFormat();
callFormat.setAlignment(Alignment.CENTRE);
callFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
callFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
Iterator columnIterator = argVectorData.iterator(); // 列的叠带器
Vector rowVector = null; // 行的向量对象
int rowNum = 3; // 值起始行号
for (; columnIterator.hasNext();) {
rowVector = (Vector) columnIterator.next();
for (int i = 0, n = rowVector.size(); i < n; i++) {
ws.addCell(new Label(i, rowNum, rowVector.elementAt(i)
.toString(), callFormat));
}
rowNum++;
}
// 写出Excel文件
excelModel.write(); // 必须调用写文件的方法
} catch (FileNotFoundException fne) {
fne.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} catch (WriteException wex) {
wex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 设置Excel报表标题
*
* @param sheet
* Excel操作对象的Sheet
* @param argTitle
* 标题名称
* @param colunmUnite
* 合并列数
* @param rowUnite
* 合并行数
* @return WritableSheet
* @throws WriteException
*/
protected WritableSheet setExcelTitle(WritableSheet sheet, String argTitle,
int colunmUnite, int rowUnite) throws WriteException {
// 设置合并标题栏的格式
sheet.mergeCells(0, 0, colunmUnite, rowUnite);
// 定义标题字体等信息
WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 20,
WritableFont.NO_BOLD);
WritableCellFormat titleFormat = new WritableCellFormat(titleFont);
titleFormat.setAlignment(Alignment.CENTRE);
titleFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
Label title = new Label(0, 0, argTitle// 设置的标题
, titleFormat);
sheet.addCell(title);
return sheet;
}
/**
* 生成销售对帐单Excel报表的报表头部
*
* @param sheet
* WritableSheet
* @return WritableSheet
* @throws WriteException
*/
public WritableSheet buildExlHead(WritableSheet sheet, String argTitle)
throws Exception {
try {
setExcelTitle(sheet, argTitle, 4, 0);
WritableFont headFont = new WritableFont(WritableFont.ARIAL, 12,
WritableFont.NO_BOLD);
WritableCellFormat headFormat = new WritableCellFormat(headFont);
headFormat.setAlignment(Alignment.CENTRE);
headFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
headFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
sheet.mergeCells(0, 1, 0, 2); // 0 列的起始数 1 行的起始数
Label d1 = new Label(0, 1, "消费流水号", headFormat);
sheet.addCell(d1);
sheet.mergeCells(1, 1, 1, 2);
Label d2 = new Label(1, 1, "商品名称", headFormat);
sheet.addCell(d2);
sheet.mergeCells(2, 1, 2, 2);
Label d3 = new Label(2, 1, "消费数目", headFormat);
sheet.addCell(d3);
sheet.mergeCells(3, 1, 3, 2);
Label d4 = new Label(3, 1, "消费金额", headFormat);
sheet.addCell(d4);
sheet.mergeCells(4, 1, 4, 2);
Label d5 = new Label(4, 1, "消费时间", headFormat);
sheet.addCell(d5);
return sheet;
} catch (WriteException ex) {
ex.printStackTrace();
}
return sheet;
}
// private static boolean hasGoodsConsumedInfo(String value) {
// return new GoodsConsumedDao().hasGoodsConsumedInfo(value);
// }
// 通过消费流水号取得相应的商品消费信息记录
private static Vector getGoodsConsumedInfo(String value) {
Vector v = new GoodsConsumedDao().getGoodsConsumedInfo(value);
return v;
}
static public Vector getVectorData(String rOO) {
Vector v = null;
v = getGoodsConsumedInfo(rOO);
Vector veData = new Vector();
Iterator ite = v.iterator();
while (ite.hasNext()) {
GoodsConsumedInfo gooci = (GoodsConsumedInfo) ite.next();
Vector vd = new Vector();
vd.add(String.valueOf(gooci.getConsumedId()));
vd.add(gooci.getGoodsName());
vd.add(String.valueOf(gooci.getConsumedNumber()));
vd.add(String.valueOf(gooci.getConsumedMoney()));
vd.add(gooci.getConsumedTime());
veData.add(vd);
}
return veData;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -