获取数据并且画表格方法,若从前台调用,用以下方式
window.location.href=”${_baseUrl}/dd/exportDoc;
public void exportDoc(HttpServletResponse response){
documentService.setUnlimitedLicense();
try {
//创建document对象
Document document = new Document();
DocumentBuilder builder = new DocumentBuilder(document);
builder.moveToDocumentEnd();
// 设置文档方向--横向
builder.getPageSetup().setOrientation(Orientation.LANDSCAPE);
// 这里写内容……
builder.getParagraphFormat().setAlignment(1);
builder.writeln("表格名称");
builder.startTable();
HttpSession session = request.getSession();
String tableJson = (String) session.getAttribute("tableJson");
session.removeAttribute("tableJson");
String[] cellArray = {};
String[] rowArray = {};
String[] colArray = tableJson.split(";"); //先将字符串转换为列数组,每个元素为一行
double fontSize = 5;
Map rowSpanMap = new HashMap();
for(int i = 0;i
rowArray = colArray[i].split("\\*"); //在把列数组的元素转换为行数组,行数组每一个元素为单元格
for(int j = 0;j
cellArray = rowArray[j].split("&"); //每个单元格有四个属性,分别为:内容,合并列数,合并行数,颜色
if(i<1){ //第一行需要进行水平单元格合并
int colSpan = Integer.parseInt(cellArray[1]);
if(colSpan>1){
builder.insertCell();
builder.getCellFormat().clearFormatting();
//设置单元格的背景颜色或者字体颜色
builder.getFont().clearFormatting();
if("red".equals(cellArray[3])){ //样式如果是red,则修改单元格的字体为red
builder.getFont().setColor(Color.RED);
}else if(cellArray[3].startsWith("#")){ //如果以#开始,说明是bgColor单元格的背景色,设置单元格的背景色为对应颜色
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.decode(cellArray[3]));
}else{
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.WHITE);
}
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER); //垂直居中对齐
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER); //水平居中对齐
builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
builder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
builder.getCellFormat().setVerticalMerge(CellMerge.NONE);
builder.getFont().setSize(fontSize);
builder.write(cellArray[0].equals("no")?"":cellArray[0]);
for(int k = 0;k
builder.insertCell();
builder.getCellFormat().clearFormatting();
builder.getFont().clearFormatting();
if("red".equals(cellArray[3])){ //样式如果是red,则修改单元格的字体为red
builder.getFont().setColor(Color.RED);
}else if(cellArray[3].startsWith("#")){ //如果以#开始,说明是bgColor单元格的背景色,设置单元格的背景色为对应颜色
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.decode(cellArray[3]));
}else{
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.WHITE);
}
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER); //垂直居中对齐
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER); //水平居中对齐
builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
builder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
builder.getCellFormat().setVerticalMerge(CellMerge.NONE);
builder.getFont().setSize(fontSize);
}
}else{
builder.insertCell();
builder.getCellFormat().clearFormatting();
builder.getFont().clearFormatting();
if("red".equals(cellArray[3])){ //样式如果是red,则修改单元格的字体为red
builder.getFont().setColor(Color.RED);
}else if(cellArray[3].startsWith("#")){ //如果以#开始,说明是bgColor单元格的背景色,设置单元格的背景色为对应颜色
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.decode(cellArray[3]));
}else{
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.WHITE);
}
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER); //垂直居中对齐
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER); //水平居中对齐
builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
builder.getCellFormat().setHorizontalMerge(CellMerge.NONE);
builder.getCellFormat().setVerticalMerge(CellMerge.NONE);
builder.getFont().setSize(fontSize);
builder.write(cellArray[0].equals("no")?"":cellArray[0]);
}
}else{ //后面的单元格需要进行垂直方向的合并
int rowSpan = Integer.parseInt(cellArray[2]);
if(rowSpan>1){
rowSpanMap.put(j, rowSpan+i);
builder.insertCell();
builder.getCellFormat().clearFormatting();
builder.getFont().clearFormatting();
if("red".equals(cellArray[3])){ //样式如果是red,则修改单元格的字体为red
builder.getFont().setColor(Color.RED);
}else if(cellArray[3].startsWith("#")){ //如果以#开始,说明是bgColor单元格的背景色,设置单元格的背景色为对应颜色
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.decode(cellArray[3]));
}else{
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.WHITE);
}
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER); //垂直居中对齐
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER); //水平居中对齐
builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
builder.getCellFormat().setVerticalMerge(CellMerge.FIRST);
builder.getCellFormat().setHorizontalMerge(CellMerge.NONE);
builder.getFont().setSize(fontSize);
builder.write(cellArray[0].equals("no")?"":cellArray[0]);
}else{
int rowNumber = rowSpanMap.get(j)==null?0:rowSpanMap.get(j);
if(i.insertCell ();
builder.getCellFormat().clearFormatting();
builder.getFont().clearFormatting();
if("red".equals(cellArray[3])){ //样式如果是red,则修改单元格的字体为red
builder.getFont().setColor(Color.RED);
}else if(cellArray[3].startsWith("#")){ //如果以#开始,说明是bgColor单元格的背景色,设置单元格的背景色为对应颜色
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.decode(cellArray[3]));
}else{
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.WHITE);
}
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER); //垂直居中对齐
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER); //水平居中对齐
builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
builder.getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);
builder.getCellFormat().setHorizontalMerge(CellMerge.NONE);
builder.getFont().setSize(fontSize);
}else{
builder.insertCell();
builder.getCellFormat().clearFormatting();
builder.getFont().clearFormatting();
if("red".equals(cellArray[3])){ //样式如果是red,则修改单元格的字体为red
builder.getFont().setColor(Color.RED);
}else if(cellArray[3].startsWith("#")){ //如果以#开始,说明是bgColor单元格的背景色,设置单元格的背景色为对应颜色
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.decode(cellArray[3]));
}else{
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.WHITE);
}
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER); //垂直居中对齐
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER); //水平居中对齐
builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
builder.getCellFormat().setVerticalMerge(CellMerge.NONE);
builder.getCellFormat().setHorizontalMerge(CellMerge.NONE);
builder.getFont().setSize(fontSize);
builder.write(cellArray[0].equals("no")?"":cellArray[0]);
}
}
}
}
builder.endRow();
}
builder.endTable();
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
document.save(outputStream, SaveFormat.DOCX);
documentOperateService.downLoadBrowser(request, response, "表格名称", outputStream);
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
导出浏览器提示,可选择目录下载
public void downLoadBrowser(HttpServletRequest request, HttpServletResponse response, String fileName, OutputStream outputStream){
ByteArrayOutputStream byteArrayOutputStream = null;
OutputStream outputStreamDownload = null;
try {
response.reset();
response.resetBuffer();
if (request.getHeader("user-agent").toLowerCase().contains("msie")||request.getHeader("user-agent").toLowerCase().contains("like gecko") ) {
response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName.getBytes("GB2312"), "ISO8859-1" ) +".docx"+ "\"");
}else{
response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName.getBytes("utf-8"), "ISO8859-1" ) +".docx"+ "\"");
}
response.setContentType("application/octet-stream");
response.setContentType("text/html;charset=UTF-8");
byteArrayOutputStream = (ByteArrayOutputStream) outputStream;
byte[] date = byteArrayOutputStream.toByteArray();
outputStreamDownload = response.getOutputStream();
outputStreamDownload.write(date);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(byteArrayOutputStream != null){
byteArrayOutputStream.close();
}
if(outputStreamDownload != null){
outputStreamDownload.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}