数据库表数据导出word表格

//controller方法

@RequestMapping(value="/exportQsDoucentIssuedRecord")

@SystemControllerLog(description="测试导出表")
public void exportQsDoucentIssuedRecord(HttpServletRequest request ,HttpServletResponse response){
QsDocumentsIssued qsDocumentsIssued=new QsDocumentsIssued();
List<QsDocumentsIssued> qsDocumentsIssuedList=qsDocumentsIssuedService.getAll(qsDocumentsIssued);
if(qsDocumentsIssuedList!=null&&!qsDocumentsIssuedList.isEmpty()){
try {
DocCreateWord.CreateQsDocumentsIssuedWord(qsDocumentsIssuedList, request, response);
} catch (Exception e) {
e.printStackTrace();
}
}

}

//service方法

public static void CreateQsDocumentsIssuedWord(List<QsDocumentsIssued> qsDocumentsIssuedList,

HttpServletRequest request,HttpServletResponse response) throws Exception {


response.setContentType("application/octet-stream;charset=ISO8859-1");
String  fileName = "文件发放回收登记表.doc";
String fileNameDown = new String(fileName.getBytes(), "ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename=" + fileNameDown);

WordTemplete wt = new WordTemplete();
wt.openDocument(response.getOutputStream());

wt.insertTitle("文件发放回收登记表", 12, Font.BOLD, Element.ALIGN_CENTER);
wt.insertQsDocumentsIssued(qsDocumentsIssuedList);
wt.closeDocument();

}

//text在word中组装表格

import com.lowagie.text.Anchor;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;

public void insertQsDocumentsIssued(List<QsDocumentsIssued> qsDocumentsIssuedList){
        try {
        //纸张横向
          Rectangle pageSize = new Rectangle(PageSize.A4.getHeight(), PageSize.A4.getWidth());
              pageSize.rotate();
              document.setPageSize(pageSize);
              //组装列名
Table aTable = new Table(11,qsDocumentsIssuedList.size());
int width[] = { 10,35,30,25,25,25,25,25,25,25,25 };
aTable.setWidths(width);// 设置每列所占比例
aTable.setWidth(100); // 占页面宽度 90%
aTable.setAlignment(Element.ALIGN_CENTER);// 居中显示
aTable.setAlignment(Element.ALIGN_MIDDLE);// 纵向居中显示
aTable.setAutoFillEmptyCells(true); // 自动填满
aTable.setBorderWidth(0); // 边框宽度
aTable.setBorderColor(new Color(0, 125, 255)); // 边框颜色


Font fontChinese = new Font(bfChinese, 10, Font.BOLD);
Cell cell = new Cell(new Phrase("序号", fontChinese));
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setBorderColor(new Color(0, 0, 0));
//cell.setBackgroundColor(new Color(204, 153, 255));
aTable.addCell(cell);


Cell cell1 = new Cell(new Phrase("科室", fontChinese));
cell1.setVerticalAlignment(Element.ALIGN_CENTER);
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1.setBorderColor(new Color(0, 0, 0));
//cell1.setBackgroundColor(new Color(204, 153, 255));
aTable.addCell(cell1);


Cell cell2 = new Cell(new Phrase("文件/资料名称", fontChinese));
cell2.setVerticalAlignment(Element.ALIGN_CENTER);
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setBorderColor(new Color(0, 0, 0));
//cell2.setBackgroundColor(new Color(204, 153, 255));
aTable.addCell(cell2);

Cell cell3 = new Cell(new Phrase("文件/资料编号", fontChinese));
cell3.setVerticalAlignment(Element.ALIGN_CENTER);
cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
cell3.setBorderColor(new Color(0, 0, 0));
//cell2.setBackgroundColor(new Color(204, 153, 255));
aTable.addCell(cell3);

Cell cell4 = new Cell(new Phrase("发放编号", fontChinese));
cell4.setVerticalAlignment(Element.ALIGN_CENTER);
cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
cell4.setBorderColor(new Color(0, 0, 0));
//cell2.setBackgroundColor(new Color(204, 153, 255));
aTable.addCell(cell4);

Cell cell5 = new Cell(new Phrase("受控状态", fontChinese));
cell5.setVerticalAlignment(Element.ALIGN_CENTER);
cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
cell5.setBorderColor(new Color(0, 0, 0));
//cell5.setBackgroundColor(new Color(204, 153, 255));
aTable.addCell(cell5);

Cell cell6 = new Cell(new Phrase("持有者", fontChinese));
cell6.setVerticalAlignment(Element.ALIGN_CENTER);
cell6.setHorizontalAlignment(Element.ALIGN_CENTER);
cell6.setBorderColor(new Color(0, 0, 0));
//cell6.setBackgroundColor(new Color(204, 153, 255));
aTable.addCell(cell6);

Cell cell7 = new Cell(new Phrase("领用时间", fontChinese));
cell7.setVerticalAlignment(Element.ALIGN_CENTER);
cell7.setHorizontalAlignment(Element.ALIGN_CENTER);
cell7.setBorderColor(new Color(0, 0, 0));
//cell7.setBackgroundColor(new Color(204, 153, 255));
aTable.addCell(cell7);

Cell cell8 = new Cell(new Phrase("回收时间", fontChinese));
cell8.setVerticalAlignment(Element.ALIGN_CENTER);
cell8.setHorizontalAlignment(Element.ALIGN_CENTER);
cell8.setBorderColor(new Color(0, 0, 0));
//cell8.setBackgroundColor(new Color(204, 153, 255));
aTable.addCell(cell8);

Cell cell9 = new Cell(new Phrase("回收人", fontChinese));
cell9.setVerticalAlignment(Element.ALIGN_CENTER);
cell9.setHorizontalAlignment(Element.ALIGN_CENTER);
cell9.setBorderColor(new Color(0, 0, 0));
//cell9.setBackgroundColor(new Color(204, 153, 255));
aTable.addCell(cell9);

Cell cell10 = new Cell(new Phrase("回收状况", fontChinese));
cell10.setVerticalAlignment(Element.ALIGN_CENTER);
cell10.setHorizontalAlignment(Element.ALIGN_CENTER);
cell10.setBorderColor(new Color(0, 0, 0));
//cell10.setBackgroundColor(new Color(204, 153, 255));
aTable.addCell(cell10);

int fileSum = 0 ;
Int2ChRule int2ChRule = new Int2ChRule() ;
if(qsDocumentsIssuedList != null ){
for (int i=0,len=qsDocumentsIssuedList.size();i<len;i++) {

aTable.addCell(new Cell(i+""));
aTable.addCell(new Cell(qsDocumentsIssuedList.get(i).getDepartment())); 
aTable.addCell(new Cell(qsDocumentsIssuedList.get(i).getFileName()));

aTable.addCell(new Cell(qsDocumentsIssuedList.get(i).getFileCode()));
aTable.addCell(new Cell(qsDocumentsIssuedList.get(i).getIssuedCode().toString())); 
String controlledStatus="";
if(qsDocumentsIssuedList.get(i).getControlledState().compareTo(new BigDecimal(0))==0){
controlledStatus="非受控";
}else if(qsDocumentsIssuedList.get(i).getControlledState().compareTo(new BigDecimal(0))==1){
controlledStatus="受控";
}
aTable.addCell(new Cell(controlledStatus));

aTable.addCell(new Cell(qsDocumentsIssuedList.get(i).getOwner()));
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
aTable.addCell(new Cell(sdf.format(qsDocumentsIssuedList.get(i).getIssuedTime()))); 
String receiveTime="";
if(qsDocumentsIssuedList.get(i).getRetrieveTime()!=null){
receiveTime=sdf.format(qsDocumentsIssuedList.get(i).getRetrieveTime());
}
aTable.addCell(new Cell(receiveTime));

aTable.addCell(new Cell(qsDocumentsIssuedList.get(i).getRetrieveUser()));
aTable.addCell(new Cell(qsDocumentsIssuedList.get(i).getWithDrawState())); 

}
}


document.add(aTable);
document.add(new Paragraph("\n"));

} catch (BadElementException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}

猜你喜欢

转载自blog.csdn.net/zyj_princess/article/details/80970812
今日推荐