java中的displaytag类_java-如何在displaytag中以xlsx扩展名导出Excel文...
這是一個非常有用的問題.
步驟1:制作一個com.displaytag.export.views包
步驟2:在上述包中創建一個類myExcel2007ExportView.java.
步驟3:將以下代碼復制并粘貼到該文件中
package com.displaytag.export.views;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import javax.servlet.jsp.JspException;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.displaytag.export.BinaryExportView;
import org.displaytag.model.Column;
import org.displaytag.model.ColumnIterator;
import org.displaytag.model.HeaderCell;
import org.displaytag.model.RowIterator;
import org.displaytag.model.TableModel;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.lowagie.text.BadElementException;
public class myExcel2007ExportView implements BinaryExportView{
//private static Log log = LogFactory.getLog(myExcel2007ExportView.class);
private TableModel model;
private boolean exportFull;
private boolean header;
private boolean decorated;
private XSSFWorkbook workbook;
private XSSFSheet sheet;
private int rowCount=0;
private CellStyle normalstyle=null;
private CellStyle headerstyle=null;
private CellStyle captionstyle=null;
private XSSFFont defaultFont;
private XSSFFont headerFont;
private XSSFFont captionFont;
public String getMimeType() {
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
}
public void setParameters(TableModel tableModel, boolean exportFullList, boolean includeHeader,
boolean decorateValues)
{
this.model = tableModel;
this.exportFull = true;
this.header = includeHeader;
this.decorated = decorateValues;
}
public boolean outputPage() {
return false;
}
protected void initTable() throws BadElementException
{
//Blank workbook
workbook = new XSSFWorkbook();
//Create a blank sheet
sheet = workbook.createSheet("Server Management System");
defaultFont= workbook.createFont();
defaultFont.setFontHeightInPoints((short)10);
defaultFont.setFontName("Arial");
defaultFont.setColor(IndexedColors.BLACK.getIndex());
defaultFont.setBold(false);
defaultFont.setItalic(false);
headerFont= workbook.createFont();
headerFont.setFontHeightInPoints((short)10);
headerFont.setFontName("Arial");
headerFont.setColor(IndexedColors.WHITE.getIndex());
headerFont.setBold(true);
headerFont.setItalic(false);
captionFont= workbook.createFont();
captionFont.setFontHeightInPoints((short)15);
captionFont.setFontName("Arial");
captionFont.setColor(IndexedColors.BLACK.getIndex());
captionFont.setBold(true);
captionFont.setItalic(false);
normalstyle= workbook.createCellStyle();
normalstyle.setFillBackgroundColor(IndexedColors.WHITE.getIndex());
normalstyle.setFillPattern(CellStyle.BIG_SPOTS);
normalstyle.setAlignment(CellStyle.ALIGN_CENTER);
normalstyle.setFont(defaultFont);
headerstyle= workbook.createCellStyle();
headerstyle.setFillBackgroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
headerstyle.setFillPattern(CellStyle.BIG_SPOTS);
headerstyle.setAlignment(CellStyle.ALIGN_CENTER);
headerstyle.setFont(headerFont);
captionstyle= workbook.createCellStyle();
captionstyle.setFillBackgroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
captionstyle.setFillPattern(CellStyle.BIG_SPOTS);
captionstyle.setAlignment(CellStyle.ALIGN_CENTER);
captionstyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
captionstyle.setFont(captionFont);
}
protected void generateXLSXTable() throws JspException, BadElementException
{
//adding caption
if(this.model.getCaption()!=null && this.model.getCaption()!="")
{
Row blankrow = sheet.createRow(rowCount++);
Row row = sheet.createRow(rowCount++);
row.setHeight((short)5000);
int columnCount=0;
Cell captionCell = row.createCell(columnCount++);
captionCell.setCellValue(this.model.getCaption());
captionCell.setCellStyle(captionstyle);
sheet.addMergedRegion(new CellRangeAddress(
rowCount-1, //first row (0-based)
rowCount-1, //last row (0-based)
columnCount-1, //first column (0-based)
columnCount+5 //last column (0-based)
));
Row blankrow2 = sheet.createRow(rowCount++);
}
if (this.header)
{
generateHeaders();
}
generateRows();
//adding footer
if(this.model.getFooter()!=null && this.model.getFooter()!="")
{
Row blankrow = sheet.createRow(rowCount++);
Row blankrow2 = sheet.createRow(rowCount++);
Row row = sheet.createRow(rowCount++);
row.setHeight((short)1000);
int columnCount=0;
Cell captionCell = row.createCell(columnCount++);
captionCell.setCellValue(this.model.getFooter());
captionCell.setCellStyle(captionstyle);
sheet.addMergedRegion(new CellRangeAddress(
rowCount-1, //first row (0-based)
rowCount-1, //last row (0-based)
columnCount-1, //first column (0-based)
columnCount+5 //last column (0-based)
));
}
}
protected void generateHeaders() throws BadElementException
{
Iterator iterator = this.model.getHeaderCellList().iterator();
Row row = sheet.createRow(rowCount++);
int columnCount=0;
while (iterator.hasNext())
{
sheet.autoSizeColumn(columnCount);
HeaderCell headerCell = iterator.next();
String columnHeader = headerCell.getTitle();
if (columnHeader == null)
{
columnHeader = StringUtils.capitalize(headerCell.getBeanPropertyName());
}
Cell hdrCell = row.createCell(columnCount++);
hdrCell.setCellValue(columnHeader);
hdrCell.setCellStyle(headerstyle);
}
}
protected void generateRows() throws JspException, BadElementException
{
// get the correct iterator (full or partial list according to the exportFull field)
RowIterator rowIterator = this.model.getRowIterator(this.exportFull);
// iterator on rows
while (rowIterator.hasNext())
{
Row newrow = sheet.createRow(rowCount++);
org.displaytag.model.Row row = rowIterator.next();
// iterator on columns
ColumnIterator columnIterator = row.getColumnIterator(this.model.getHeaderCellList());
int columnCount=0;
while (columnIterator.hasNext())
{
Column column = columnIterator.nextColumn();
// Get the value to be displayed for the column
Object value = column.getValue(this.decorated);
Cell Cell = newrow.createCell(columnCount++);
Cell.setCellValue(ObjectUtils.toString(value));
}
}
}
@Override
public void doExport(OutputStream out) throws IOException, JspException {
try {
// Initialize the table with the appropriate number of columns
initTable();
generateXLSXTable();
} catch (BadElementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
workbook.write(out);
out.close();
}
}
注意:您可以根據需要修改此類.
步驟4:在資源文件夾中創建一個文件displaytag.properties
步驟5:在其中復制并粘貼以下代碼
export.amount=list
sort.amount=list
export.decorated=true
export.types=csv excel xml pdf rtf [mymedia]
export.[mymedia]=true
export.[mymedia].class=com.displaytag.export.views.myExcel2007ExportView
export.excel=true
export.csv=true
export.xml=true
export.pdf=true
export.pdf.include_header=true
export.excel.include_header=true
export.rtf=true
export.rtf.label=RTF
export.rtf.include_header=true
export.rtf.filename=
export.[mymedia].label=Excel 2007
export.[mymedia].include_header=true
export.[mymedia].filename=
注意:請確保您不更改屬性的順序
步驟6:在JSP文件中
yourcaption//other columns
yourfooter
注意:不要忘記在標題,頁腳中添加[mymedia],也不要忘記按如下所示設置文件名.
步驟7:將以下jar文件復制并粘貼到WEB-INF / lib文件夾中
displaytag-1.2.jar
displaytag-export-poi-1.2.jar
displaytag-portlet-1.2.jar
commons-lang-2.3 or above
commons-beanutils-1.7.0 or above
commons-collections-3.1 or above
commons-logging-1.1.jar
itext-1.3 or above
slf4j-api-1.4.2 or above
slf4j-log4j12-1.4.2 or above
poi-3.2-FINAL.jar
poi-3.9.jar
poi-ooxml-3.9.jar
poi-ooxml-schemas-3.9.jar
stax-api-1.0.1.jar
xmlbeans-2.6.0.jar
commons-codec-1.5.jar
dom4j-1.6.1.jar
log4j-1.2.15.jar
注意:如果我忘記了任何依賴項jar,請使用maven進行檢查.
步驟8:運行項目并享受():)如果有任何錯誤,也請通知我.另外,如果您在displaytag中使用任何裝飾器,則必須為其編寫代碼.
謝謝
制作資源文件夾如果您使用的是eclipse,請右鍵單擊以下Java資源
提供名稱res并在其中創建屬性文件
使用netBeans IDE 7.4
總結
以上是生活随笔為你收集整理的java中的displaytag类_java-如何在displaytag中以xlsx扩展名导出Excel文...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【AtCoder】【模拟】【模型转化】C
- 下一篇: excel----身份证号校验位exce