poi export word example

an simple example of poi to export word

public String ExportWord() {
try {
String orgtype = request.getSession().getAttribute(“session_orgtype”).toString();
List<DdzhDdrzXhgzDwcVo> vo_list = new ArrayList<DdzhDdrzXhgzDwcVo>();
if (xhgzqo == null) {
xhgzqo = new DdzhDdrzXhgzQo();
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -1);
xhgzqo.setBeginFssj(calendar.getTime());
xhgzqo.setEndFssj(Calendar.getInstance().getTime());
}
if (orgtype != null && orgtype.equals(“”) == false) {
if (orgtype.substring(0, 2).endsWith(“1J”)) {
if (xhgzqo.getBeginFssjStr() != null) {
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm”);
xhgzqo.setBeginFssj(sdf.parse(xhgzqo.getBeginFssjStr()));
}
if (xhgzqo.getEndFssjStr() != null) {
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm”);
xhgzqo.setEndFssj(sdf.parse(xhgzqo.getEndFssjStr()));
}
xhgzqo.setShangbaoFlag(“0”);
xhgzqo.setIsDealFlag(“1”);
vo_list = ddzhDdrzXhgzDwcService.findDwcByCondition(xhgzqo);
} else {
xhgzqo.setShangbaoFlag(“1”);
if (xhgzqo.getBeginFssjStr() != null) {
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm”);
xhgzqo.setBeginFssj(sdf.parse(xhgzqo.getBeginFssjStr()));
}
if (xhgzqo.getEndFssjStr() != null) {
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm”);
xhgzqo.setEndFssj(sdf.parse(xhgzqo.getEndFssjStr()));
}
if (xhgzqo.getZkbdFlag() != null
&& xhgzqo.getZkbdFlag().equals(“”) == false
&& xhgzqo.getZkbdFlag().equals(“1”)) {
xhgzqo.setDjlFlag(null);
xhgzqo.setJlrUnitDuanId(request.getSession().getAttribute(“session_duanid”).toString());
} else {
xhgzqo.setDjlFlag(null);
xhgzqo.setJlrUnitDuanId(null);
}
vo_list = ddzhDdrzXhgzService.findXhgzByCondition(xhgzqo);
}
}

String orgnum = (String) request.getSession().getAttribute(“session_orgnum_nodep”);//session_duanid
List duanList = OrgService.getDuanJuNumList(orgnum);
DdrzQo ddrzQo = new DdrzQo();
Calendar beginCalendar = Calendar.getInstance();
beginCalendar.set(new Integer(xhgzqo.getBeginFssjStr().substring(0, 4)),
new Integer(xhgzqo.getBeginFssjStr().substring(5, 7)),
new Integer(xhgzqo.getBeginFssjStr().substring(8, 10)),
new Integer(xhgzqo.getBeginFssjStr().substring(11, 13)),
new Integer(xhgzqo.getBeginFssjStr().substring(14, 16)));
ddrzQo.setBeginFssj(beginCalendar.getTime());
Calendar endCalendar = Calendar.getInstance();
endCalendar.set(new Integer(xhgzqo.getEndFssjStr().substring(0, 4)),
new Integer(xhgzqo.getEndFssjStr().substring(5, 7)),
new Integer(xhgzqo.getEndFssjStr().substring(8, 10)),
new Integer(xhgzqo.getEndFssjStr().substring(11, 13)),
new Integer(xhgzqo.getEndFssjStr().substring(14, 16)));
ddrzQo.setEndFssj(endCalendar.getTime());
List<DdzhDdrzJccr> jccrList = jccrService.findJccrByCondition(ddrzQo);

Map<String, String> tcqo = new HashMap<String, String>();
tcqo.put(“q_beginTime”, xhgzqo.getBeginFssjStr().substring(0,10));
tcqo.put(“q_endTime”, xhgzqo.getEndFssjStr().substring(0,10));
List tcList = tcdao.getTcByCondition(tcqo);

//export word
XWPFDocument document = new XWPFDocument();
XWPFParagraph headpara = document.createParagraph();
headpara.setAlignment(ParagraphAlignment.CENTER);
XWPFRun headRun = headpara.createRun();
headRun.setFontSize(15);
headRun.setText(“信号故障一览”);
XWPFTable table = null;
// if (vo_list == null || vo_list.size() == 0) {
table = document.createTable(6,12);
// }else {
// table = document.createTable(5+vo_list.size(),12);
// }
setTableWidth(table, “9000”);
XWPFTableCell c00 = table.getRow(0).getCell(0);
mergeCellsVertically(table, 0, 0, 1);
setCellText(c00,”序号”,ParagraphAlignment.CENTER);
XWPFTableCell c01 = table.getRow(0).getCell(1);
mergeCellsVertically(table, 1, 0, 1);
setCellText(c01,”车站”,ParagraphAlignment.CENTER);
XWPFTableCell c02 = table.getRow(0).getCell(2);
mergeCellsHorizontal(table, 0, 2, 5);
setCellText(c02,”时间”,ParagraphAlignment.CENTER);
XWPFTableCell c06 = table.getRow(0).getCell(6);
mergeCellsHorizontal(table, 0, 6, 8);
mergeCellsHorizontal(table, 1, 6, 8);
mergeCellsVertically(table, 6, 0, 1);
mergeCellsVertically(table, 7, 0, 1);
mergeCellsVertically(table, 8, 0, 1);
setCellText(c06,”故障现象、原因及处理过程”,ParagraphAlignment.CENTER);
XWPFTableCell c09 = table.getRow(0).getCell(9);
mergeCellsVertically(table, 9, 0, 1);
setCellText(c09,”影响列车情况”,ParagraphAlignment.CENTER);
XWPFTableCell c0A = table.getRow(0).getCell(10);
mergeCellsVertically(table, 10, 0, 1);
setCellText(c0A,”定性”,ParagraphAlignment.CENTER);
XWPFTableCell c0B = table.getRow(0).getCell(11);
mergeCellsVertically(table, 11, 0, 1);
setCellText(c0B,”原因”,ParagraphAlignment.CENTER);
XWPFTableCell c12 = table.getRow(1).getCell(2);
mergeCellsHorizontal(table, 1, 2, 3);
setCellText(c12,”发生/修复”,ParagraphAlignment.CENTER);
XWPFTableCell c14 = table.getRow(1).getCell(4);
mergeCellsHorizontal(table, 1, 4, 5);
setCellText(c14,”通知/到达”,ParagraphAlignment.CENTER);
int xhgzSize = 0;
if (vo_list != null && vo_list.size() > 0) {
xhgzSize = vo_list.size();
for (int i = 0; i < xhgzSize; i++) {
DdzhDdrzXhgzDwcVo vo = vo_list.get(i);
XWPFTableRow row = null;
if (i == 0) {
row = table.getRow(2+i);
}else if (i < xhgzSize) {
row = table.insertNewTableRow(i+2);
for (int j = 0; j < 12; j++) {
row.addNewTableCell();
}
}
XWPFTableCell ci0 = row.getCell(0);
setCellText(ci0,new Integer(i+1).toString(),ParagraphAlignment.CENTER);
XWPFTableCell ci1 = row.getCell(1);
setCellText(ci1,vo.getDidianName()+”/”+vo.getLineName(),ParagraphAlignment.CENTER);
XWPFTableCell ci2 = row.getCell(2);
mergeCellsHorizontal(table, 2+i, 2, 3);
setCellText(ci2,vo.getFssj()+”/”+vo.getXfsj(),ParagraphAlignment.CENTER);
XWPFTableCell ci4 = row.getCell(4);
mergeCellsHorizontal(table, 2+i, 4, 5);
setCellText(ci4,vo.getTzsj()+”/”+vo.getDdsj(),ParagraphAlignment.CENTER);
XWPFTableCell ci6 = row.getCell(6);
mergeCellsHorizontal(table, 2+i, 6, 8);
String i4text = “”;
if (vo.getGzxxContent() != null && !”null”.equals(vo.getGzxxContent())) {
i4text = i4text + vo.getGzxxContent();
}
if (vo.getYyfxContent() != null && !”null”.equals(vo.getYyfxContent())) {
i4text = i4text + vo.getYyfxContent();
}
if (vo.getClgcContent() != null && !”null”.equals(vo.getClgcContent())) {
i4text = i4text + vo.getClgcContent();
}
ci6.setText(i4text);
XWPFTableCell ci9 = row.getCell(9);
setCellText(ci9,vo.getYxlcqk(),ParagraphAlignment.CENTER);
XWPFTableCell ciA = row.getCell(10);
setCellText(ciA,vo.getGzxzname(),ParagraphAlignment.CENTER);
XWPFTableCell ciB = row.getCell(11);
setCellText(ciB,vo.getGzReasonText(),ParagraphAlignment.CENTER);
}
}
XWPFTableCell c30 = table.getRow(xhgzSize+2).getCell(0);
mergeCellsVertically(table, 0, xhgzSize+2, xhgzSize+3);
setCellText(c30,”单位”,ParagraphAlignment.CENTER);
XWPFTableCell c31 = table.getRow(xhgzSize+2).getCell(1);
mergeCellsHorizontal(table, xhgzSize+2, 1, 2);
setCellText(c31,”出入库”,ParagraphAlignment.CENTER);
XWPFTableCell c33 = table.getRow(xhgzSize+2).getCell(3);
mergeCellsHorizontal(table, xhgzSize+2, 3, 7);
setCellText(c33,”天窗修记录”,ParagraphAlignment.CENTER);
XWPFTableCell c38 = table.getRow(xhgzSize+2).getCell(8);
mergeCellsHorizontal(table, xhgzSize+2, 8, 11);
setCellText(c38,”待办事项”,ParagraphAlignment.CENTER);
XWPFTableCell c41 = table.getRow(xhgzSize+3).getCell(1);
setCellText(c41,”出”,ParagraphAlignment.CENTER);
XWPFTableCell c42 = table.getRow(xhgzSize+3).getCell(2);
setCellText(c42,”入”,ParagraphAlignment.CENTER);
XWPFTableCell c43 = table.getRow(xhgzSize+3).getCell(3);
setCellText(c43,”站”,ParagraphAlignment.CENTER);
XWPFTableCell c44 = table.getRow(xhgzSize+3).getCell(4);
setCellText(c44,”计划次”,ParagraphAlignment.CENTER);
XWPFTableCell c45 = table.getRow(xhgzSize+3).getCell(5);
setCellText(c45,”兑现次”,ParagraphAlignment.CENTER);
XWPFTableCell c46 = table.getRow(xhgzSize+3).getCell(6);
setCellText(c46,”计划时”,ParagraphAlignment.CENTER);
XWPFTableCell c47 = table.getRow(xhgzSize+3).getCell(7);
setCellText(c47,”兑现时”,ParagraphAlignment.CENTER);
mergeCellsHorizontal(table, xhgzSize+3, 8, 11);
mergeCellsVertically(table, 8, xhgzSize+2, xhgzSize+3);
mergeCellsVertically(table, 9, xhgzSize+2, xhgzSize+3);
mergeCellsVertically(table, 10, xhgzSize+2, xhgzSize+3);
mergeCellsVertically(table, 11, xhgzSize+2, xhgzSize+3);
if (duanList != null && duanList.size() > 0) {
boolean flag = false;
int index = 0;
for (int i = 0; i < duanList.size(); i++) {
Map<String, String> duan = (Map<String, String>) duanList.get(i);
XWPFTableRow row = null;
if (duan.get(“ORGNUM”).length() != 4) {
continue;
}
if (!flag) {
flag = true;
row = table.getRow(xhgzSize+4);
mergeCellsHorizontal(table, xhgzSize+4, 8, 11);
}else {
row = table.insertNewTableRow(xhgzSize+5+index);
for (int j = 0; j < 12; j++) {
row.addNewTableCell();
}
mergeCellsHorizontal(table, xhgzSize+5+index, 8, 11);
index = index + 1;
}
XWPFTableCell c50 = row.getCell(0);
setCellText(c50,duan.get(“ORGNAME”),ParagraphAlignment.CENTER);
if (jccrList != null && jccrList.size() > 0) {
for (int j = 0; j < jccrList.size(); j++) {
if (duan.get(“ID”).equals(jccrList.get(j).getDwd_id())) {
XWPFTableCell c51 = row.getCell(1);
setCellText(c51,jccrList.get(j).getCk_count() + “”,ParagraphAlignment.CENTER);
XWPFTableCell c52 = row.getCell(2);
setCellText(c52,jccrList.get(j).getRk_count() + “”,ParagraphAlignment.CENTER);
}
}
}
if (tcList != null && tcList.size() > 0) {
for (int j = 0; j < tcList.size(); j++) {
TcVo tcvo = (TcVo) tcList.get(j);
if (duan.get(“ID”).equals(tcvo.getUnitId())) {
XWPFTableCell c53 = row.getCell(3);
setCellText(c53,tcvo.getStationCount(),ParagraphAlignment.CENTER);
XWPFTableCell c54 = row.getCell(4);
setCellText(c54,tcvo.getPlanCount() == null ? “” : tcvo.getPlanCount().toString(),ParagraphAlignment.CENTER);
XWPFTableCell c55 = row.getCell(5);
setCellText(c55,tcvo.getFinishCount() == null ? “” : tcvo.getFinishCount().toString(),ParagraphAlignment.CENTER);
XWPFTableCell c56 = row.getCell(6);
setCellText(c56,tcvo.getPlanTime() == null ? “” : tcvo.getPlanTime().toString(),ParagraphAlignment.CENTER);
XWPFTableCell c57 = row.getCell(7);
setCellText(c57,tcvo.getFinishTime() == null ? “” : tcvo.getFinishTime().toString(),ParagraphAlignment.CENTER);
}
}
}

}
}

SimpleDateFormat dateformat = new SimpleDateFormat(“yyyyMMddHHmmss”);
String realpath = ServletActionContext.getServletContext().getRealPath(“/cloudFiles/ddrz”);
File dir = new File(realpath);
if (!dir.exists()) {
dir.mkdirs();
}
dir = null;
String fileName = “DDRZ_” + dateformat.format(new Date()).toString() + “.doc”;
OutputStream out = new FileOutputStream(realpath + “/” + fileName);
document.write(out);
out.close();
this.setPath(realpath + “\\” + fileName);
this.setFileName(fileName);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
logger.info(e.getMessage());
outJsonPlainString(response, “{\”error\”:true}”);
}
return SUCCESS;
}

/***
* word 跨列合并
* @param table
* @param row
* @param fromCell
* @param toCell
*/
private void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {
for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {
XWPFTableCell cell = table.getRow(row).getCell(cellIndex);
if ( cellIndex == fromCell ) {
// The first merged cell is set with RESTART merge value
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
} else {
// Cells which join (merge) the first one, are set with CONTINUE
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
}
}
}

/***
* word 跨行合并
* @param table
* @param col
* @param fromRow
* @param toRow
*/
private void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) {
for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
if ( rowIndex == fromRow ) {
// The first merged cell is set with RESTART merge value
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
} else {
// Cells which join (merge) the first one, are set with CONTINUE
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
}
}
}

private void setTableWidth(XWPFTable table,String width){
CTTbl ttbl = table.getCTTbl();
CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr();
CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();
CTJc cTJc=tblPr.addNewJc();
cTJc.setVal(STJc.Enum.forString(“center”));
tblWidth.setW(new BigInteger(width));
tblWidth.setType(STTblWidth.DXA);
}

private void setCellText(XWPFTableCell cell,String text,ParagraphAlignment alignment) {
XWPFParagraph paragraph = cell.getParagraphs().get(0);
paragraph.setAlignment(alignment);
XWPFRun run = paragraph.createRun();
run.setText(text);
}

Unnamed QQ Screenshot20141211110136

This entry was posted in Java. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s