package com.adtech.tools;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
/**
* 报表打印工具类
*
* @date 2010-01-22
*/
public class ReportTool {
/**
* Excel编码处理
*
* @param toEncoded
* @param encoding
* @return
*/
public static String getUnicode(String toEncoded, String encoding) {
String retString = "";
if (toEncoded.equals("") || toEncoded.trim().equals("")) {
return toEncoded;
}
try {
byte[] b = toEncoded.getBytes(encoding);
sun.io.ByteToCharConverter convertor = sun.io.ByteToCharConverter
.getConverter(encoding);
char[] c = convertor.convertAll(b);
for (int i = 0; i < c.length; i++) {
retString += String.valueOf(c[i]);
}
} catch (java.io.UnsupportedEncodingException usee) {
System.out.println("不支持" + encoding + "编码方式");
usee.printStackTrace();
} catch (sun.io.MalformedInputException mfie) {
System.out.println("输入参数无效!!!");
mfie.printStackTrace();
}
return retString;
}
/**
* 处理文件名乱码
*
* @param str
* @return
*/
public static String getStr(String str) {
try {
String temp_p = str;
byte[] temp_t = temp_p.getBytes("ISO8859_1");
String temp = new String(temp_t);
return temp;
} catch (Exception e) {
e.printStackTrace();
}
return "null";
}
/**
* 定义字体大小样式
*
* @param workbook
* @param color
* 字体颜色
* @param boldweight
* 字体样式
* @param fontHeight
* 字体大小
* @return
*/
public static HSSFFont createHSSFFont(HSSFWorkbook workbook, short color,
short boldweight, int fontHeight) {
HSSFFont font = workbook.createFont();
font.setColor(HSSFFont.COLOR_NORMAL);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontHeight((short) fontHeight);
return font;
}
/**
* 定义边框样式 单元格四周都有边框
*
* @param workbook
* @param verticalAlignment
* 垂直显示
* @param alignment
* 水平显示样式
* @param top
* 上边框粗细
* @param bottom
* 下边框粗细
* @param left
* 左边框粗细
* @param right
* 右边框粗细
* @return
*/
public static HSSFCellStyle createHSSFCellStyle(HSSFWorkbook workbook,
short verticalAlignment, short alignment, int top, int bottom,
int left, int right) {
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setVerticalAlignment(verticalAlignment);
cellStyle.setAlignment(alignment);
cellStyle.setBorderBottom((short) bottom);
cellStyle.setBorderLeft((short) left);
cellStyle.setBorderRight((short) right);
cellStyle.setBorderTop((short) top);
return cellStyle;
}
/**
* 定义边框样式 单元格四周都有边框
*
* @param workbook
* @param verticalAlignment
* 垂直显示
* @param alignment
* 水平显示样式
* @param top
* 上边框粗细
* @param bottom
* 下边框粗细
* @param left
* 左边框粗细
* @param right
* 右边框粗细
* @param font
* 单元格字体样式
* @return
*/
public static HSSFCellStyle createHSSFCellStyle(HSSFWorkbook workbook,
HSSFFont font, short verticalAlignment, short alignment, int top,
int bottom, int left, int right) {
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setVerticalAlignment(verticalAlignment);
cellStyle.setAlignment(alignment);
cellStyle.setBorderBottom((short) bottom);
cellStyle.setBorderLeft((short) left);
cellStyle.setBorderRight((short) right);
cellStyle.setBorderTop((short) top);
cellStyle.setFont(font);
return cellStyle;
}
/**
* 创建行
*
* @param sheet
* 工作表
* @param rowNum
* 行号
* @param height
* 行高
*
* @return
*/
public static HSSFRow createHSSFRow(HSSFSheet sheet, int rowNum, int height) {
HSSFRow row = sheet.createRow((short) rowNum);
row.setHeight((short) height);
return row;
}
/**
* 创建列
*
* @param hssfRow
* 行对象
* @param cellStyle
* 单元格样式
* @param cellNum
* 列号
* @param content
* 单元格内容
*
*/
public static void createHSSFCell(HSSFRow hssfRow, HSSFCellStyle cellStyle,
int cellNum, String content) {
HSSFCell cell = hssfRow.createCell((short) cellNum);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStyle);
content = getUnicode(content, "gb2312");
cell.setCellValue(content);
}
/**
* 创建列
*
* @param hssfRow
* 行对象
* @param cellStyle
* 单元格样式
* @param cellNum
* 列号
* @param content
* 单元格内容
* @param beginRow
* 合并单元格开始行
* @param beginCell
* 合并单元格开始列
* @param endRow
* 合并单元格结束行
* @param endCell
* 合并单元格结束列
*
*/
public static void createHSSFCell(HSSFSheet sheet, HSSFRow hssfRow,
HSSFCellStyle cellStyle, int cellNum, String content, int beginRow,
int beginCell, int endRow, int endCell) {
HSSFCell cell = hssfRow.createCell((short) cellNum);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStyle);
content = getUnicode(content, "gb2312");
cell.setCellValue(content);
sheet.addMergedRegion(new Region((short) beginRow, (short) beginCell,
(short) endRow, (short) endCell));
for (int i = beginCell + 1; i < endCell + 1; i++) {
cell = hssfRow.createCell((short) i);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStyle);
}
if (beginRow < endRow) {
for (int j = beginRow + 1; j < endRow + 1; j++) {
HSSFRow row = ReportTool.createHSSFRow(sheet, j, 300);
for (int i = beginCell; i < endCell + 1; i++) {
cell = row.createCell((short) i);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStyle);
}
}
}
}
/**
* 设置页面返回类型
*
* @param request
* @param response
* @param jyPath
* @throws ServletException
* @throws IOException
*/
public static void setExclType(HttpServletRequest request,
HttpServletResponse response, String jyPath) {
response.setContentType("application/octet-stream");
String outputFile = request.getParameter("TEMPLET_NNAME") != null ? getUnicode(
request.getParameter("TEMPLET_NNAME"), "gb2312")
+ ".xls"
: "";
HttpSession session = request.getSession();
if (outputFile.equals("")) {
// 所有状态查询标题不能为空
session.setAttribute("ADQMS9000_MESSAGE_TYPE", "1");
session.setAttribute("ADQMS9000_MESSAGE_CONTENT", "你没有权限导出!");
session.setAttribute("ADQMS9000_MESSAGE_GOTO",
"javascript:history.back(1)");
try {
request.getRequestDispatcher(jyPath).forward(request, response);
} catch (Exception e) {
}
}
String fileName = outputFile;
response.setHeader("Content-Disposition", "attachment;filename="
+ fileName);
}
}