import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
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.HSSFRichTextString;
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.HSSFColor;
/**
* 导出Excel公共类
* <p>Title:ExportExcel </p>
* <p>Description: </p>
* <p>Company: </p>
* @author David
* @date May 31, 2016 9:49:50 AM
*/
public class ExcelExport {
//The title of the displayed export table
private String title;
//The column name of the export table
private String[] rowName ;
private List <Object[]> dataList = new ArrayList<Object[]>();
private HttpServletResponse response;
//Construction method, pass in the data to be exported
public ExcelExport(String title,String[] rowName,List<Object[]> dataList ,HttpServletResponse response){
this.dataList = dataList;
this.rowName = rowName;
this.title = title;
this.response=response;
}
/**
* Export data
* @throws Exception
*/
public void export() throws Exception{
//Create workbook object
HSSFWorkbook workbook = new HSSFWorkbook();
//Create worksheet
HSSFSheet sheet = workbook.createSheet(title);
//Get column header style object
HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);
//cell style object
HSSFCellStyle style = this.getStyle(workbook);
//merge cells
//sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, (rowName.length -1)));
// define the desired number of columns
int columnNum = rowName.length;
// create a row
HSSFRow rowRowName = sheet.createRow(0);
// Set the column header to the cell of the sheet
for(int n=0;n<columnNum;n++){
//Create the number of cells corresponding to the column header
HSSFCell cellRowName = rowRowName.createCell(n);
//Set the data type of the column header cell
cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING);
HSSFRichTextString text = new HSSFRichTextString(rowName[n]);
//Set the value of the column header cell
cellRowName.setCellValue (text);
//Set the column header cell style
cellRowName.setCellStyle(columnTopStyle);
}
//Set the queried data to the cell corresponding to the sheet
for(int i=0;i<dataList.size();i++){
//traverse each object
Object[] obj = dataList.get(i);
//create the required number of rows
HSSFRow row = sheet.createRow (i+1);
for(int j=0; j<obj.length; j++){
//Set the data type
of the cell HSSFCell cell = null;
if(j == 0){
cell = row.createCell(j ,HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(i+1);
}else{
cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING);
if(null!=obj[j]&&!"".equals(obj[j ])){
//Set the value of the cell
cell.setCellValue(obj[j].toString());
}
}
//Set the cell style
cell.setCellStyle(style);
}
}
for (int colNum = 0; colNum < columnNum; colNum++ ) {
sheet.setColumnWidth(colNum, 5000);
}
if(workbook !=null){
String fileName = "Excel-" + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls" ;
response.setHeader("Content-disposition", "attachment; filename=" + fileName);
response.setContentType("application/vnd.ms-excel;charset=utf-8");
OutputStream out = response.getOutputStream();
workbook.write(out);
}
}
/*
* column header cell style
*/
public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) {
// set font
HSSFFont font = workbook.createFont();
// set font size
font.setFontHeightInPoints(( short)11);
//font bold
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//Set the font name
font.setFontName("Courier New");
//Set the style;
HSSFCellStyle style = workbook.createCellStyle();
//Set the bottom border;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//Set the bottom border color ;
style.setBottomBorderColor(HSSFColor.BLACK.index);
//Set the left border;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//Set the left border color;
style.setLeftBorderColor(HSSFColor.BLACK.index);
//Set the right border ;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//Set the right border color;
style.setRightBorderColor(HSSFColor.BLACK.index);
//Set top border;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//Set top border color;
style.setTopBorderColor(HSSFColor.BLACK.index);
//Apply in style Set font;
style.setFont(font);
//Set automatic line wrapping;
style.setWrapText(false);
//Set the horizontal alignment style to center alignment;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//Set vertical alignment The style is center alignment;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
return style;
}
/*
* Column data information cell style
*/
public HSSFCellStyle getStyle(HSSFWorkbook workbook) {
// Set font
HSSFFont font = workbook.createFont();
//Set font size
font.setFontHeightInPoints((short)10);
//Bold font
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//Set the font name
font.setFontName("Courier New");
//Set the style;
HSSFCellStyle style = workbook.createCellStyle();
//Set the bottom border;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//Set the bottom border color ;
style.setBottomBorderColor(HSSFColor.BLACK.index);
//Set the left border;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//Set the left border color;
style.setLeftBorderColor(HSSFColor.BLACK.index);
//Set the right border;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//Set the right border color;
style .setRightBorderColor(HSSFColor.BLACK.index);
//Set the top border;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//Set the top border color;
style.setTopBorderColor(HSSFColor.BLACK.index);
//Apply settings in the style Font;
style.setFont(font);
//Set automatic line wrapping;
style.setWrapText(false);
//Set the horizontal alignment style to center alignment;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//Set the vertical alignment style to center alignment;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
return style;
}
}
//Called place
/**
* export
* @param request
* @ param response
*/
@RequestMapping(value = "/exportcustomerExcel")
public void exportcustomerExcel(HttpServletRequest request,HttpServletResponse response){
String title = "User Information";
String[] rowsName = new String[]{"Number","User Account ","nickname","mobile phone","registration date","source","role"};
List<Object[]> dataList = new ArrayList<Object[]>();
try{
UserCondition condition=UserCondition.convertCondition(request,"c.CreateTime");
List<Customer> customerList=customerService.findAllCustomer(condition);
int i=0;
for (Customer customer:customerList) {
String[] obj = new String[rowsName.length];
obj[0] = i+"";
obj[1] = customer.getLoginName();
obj[2] = customer.getNikeName();
obj[3] = customer.getMobile();
obj[4] = customer.getRegTimeStr();
obj[5] = customer.getRegFrom();
obj[6] = customer.getGroupName();
dataList.add(obj);
i++;
}
ExcelExport viewExcel = new ExcelExport(title,rowsName,dataList,response);
viewExcel.export();
}catch(Exception ce){
ce.printStackTrace();
log.error("export customer to Excel error",ce);
}
}
POI Export Excel
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326188852&siteId=291194637
Recommended
Ranking