자바 poi XSSF 엑셀 코드 내 보 내기 공유
11980 단어 javase
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.PrintSetup;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFHyperlink;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class TranOrderExport {
String sheetTitle;
List tranOrderList;
Workbook workbook;
Sheet sheet;
Map styles;
public TranOrderExport(String sheetTitle, List tranOrderList) {
this.sheetTitle = sheetTitle;
this.tranOrderList = tranOrderList;
//
initSheet();
//
creatSheetHead();
//
fillSheetData();
}
/**
*
*/
private void initSheet() {
workbook = new XSSFWorkbook();
styles = createStyles(workbook);
sheet = workbook.createSheet(sheetTitle);
sheet.setDisplayGridlines(false);
sheet.setPrintGridlines(false);
sheet.setFitToPage(true);
sheet.setHorizontallyCenter(true);
sheet.createFreezePane(0, 2, 0, 2);
PrintSetup printSetup = sheet.getPrintSetup();
printSetup.setLandscape(true);
//
sheet.setColumnWidth(0, 24 * 256);
sheet.setColumnWidth(1, 12 * 256);
sheet.setColumnWidth(2, 12 * 256);
sheet.setColumnWidth(3, 12 * 256);
sheet.setColumnWidth(4, 36 * 256);
sheet.setColumnWidth(5, 12 * 256);
sheet.setColumnWidth(6, 12 * 256);
sheet.setColumnWidth(7, 12 * 256);
sheet.setColumnWidth(8, 12 * 256);
sheet.setColumnWidth(9, 24 * 256);
sheet.setColumnWidth(10, 12 * 256);
sheet.setColumnWidth(11, 12 * 256);
sheet.setColumnWidth(12, 12 * 256);
sheet.setColumnWidth(13, 12 * 256);
sheet.setColumnWidth(14, 12 * 256);
sheet.setColumnWidth(15, 12 * 256);
}
/**
*
*/
private void creatSheetHead() {
Row headRow_0 = sheet.createRow(0);
headRow_0.setHeightInPoints(20);
CellStyle headCellStyle = styles.get("head");
Cell titleCell = headRow_0.createCell(0);
titleCell.setCellValue(sheetTitle);
CellStyle headTitleStyle = styles.get("headTitle");
titleCell.setCellStyle(headTitleStyle);
//sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$AO$1"));
//
Row headRow_1 = sheet.createRow(1);
int headCellIndex = 0;
Cell headCell = headRow_1.createCell(headCellIndex++);
headCell.setCellValue(" ");
headCell.setCellStyle(headCellStyle);
headCell = headRow_1.createCell(headCellIndex++);
headCell.setCellValue(" ");
headCell.setCellStyle(headCellStyle);
headCell = headRow_1.createCell(headCellIndex++);
headCell.setCellValue(" ");
headCell.setCellStyle(headCellStyle);
headCell = headRow_1.createCell(headCellIndex++);
headCell.setCellValue(" ( )");
headCell.setCellStyle(headCellStyle);
headCell = headRow_1.createCell(headCellIndex++);
headCell.setCellValue(" ");
headCell.setCellStyle(headCellStyle);
headCell = headRow_1.createCell(headCellIndex++);
headCell.setCellValue(" ");
headCell.setCellStyle(headCellStyle);
headCell = headRow_1.createCell(headCellIndex++);
headCell.setCellValue(" ( )");
headCell.setCellStyle(headCellStyle);
headCell = headRow_1.createCell(headCellIndex++);
headCell.setCellValue(" ( )");
headCell.setCellStyle(headCellStyle);
headCell = headRow_1.createCell(headCellIndex++);
headCell.setCellValue(" ( )");
headCell.setCellStyle(headCellStyle);
headCell = headRow_1.createCell(headCellIndex++);
headCell.setCellValue(" ");
headCell.setCellStyle(headCellStyle);
headCell = headRow_1.createCell(headCellIndex++);
headCell.setCellValue(" ");
headCell.setCellStyle(headCellStyle);
headCell = headRow_1.createCell(headCellIndex++);
headCell.setCellValue(" ");
headCell.setCellStyle(headCellStyle);
headCell = headRow_1.createCell(headCellIndex++);
headCell.setCellValue(" ");
headCell.setCellStyle(headCellStyle);
headCell = headRow_1.createCell(headCellIndex++);
headCell.setCellValue(" ");
headCell.setCellStyle(headCellStyle);
headCell = headRow_1.createCell(headCellIndex++);
headCell.setCellValue(" ");
headCell.setCellStyle(headCellStyle);
headCell = headRow_1.createCell(headCellIndex++);
headCell.setCellValue(" ");
headCell.setCellStyle(headCellStyle);
}
/**
*
*/
private void fillSheetData() {
int rowNum = 2;
CellStyle cellStyle = styles.get("cell");
CellStyle urlStyle = styles.get("urlStyle");
for (int i = 0, length = tranOrderList.size(); i < length; i++) {
TranOrder record = tranOrderList.get(i);
Row row = sheet.createRow(rowNum++);
row.setHeightInPoints(2 * sheet.getDefaultRowHeightInPoints());
int cellIndex = 0;
Cell itemCell = row.createCell(cellIndex++);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String orderTime = df.format(new Date(record.getOrderTime().getTime()));
itemCell.setCellValue(fmtString(orderTime));
itemCell.setCellStyle(cellStyle);
itemCell = row.createCell(cellIndex++);
itemCell.setCellValue(fmtString(record.getOrderNO()));
itemCell.setCellStyle(cellStyle);
itemCell = row.createCell(cellIndex++);
itemCell.setCellValue(fmtString(record.getOrderStatus()));
itemCell.setCellStyle(cellStyle);
itemCell = row.createCell(cellIndex++);
itemCell.setCellValue(record.getPrice());
itemCell.setCellStyle(cellStyle);
itemCell = row.createCell(cellIndex++);
CreationHelper createHelper = workbook.getCreationHelper();
XSSFHyperlink servieLink= (XSSFHyperlink) createHelper.createHyperlink(Hyperlink.LINK_URL);
servieLink.setAddress(record.getServiceUrl());//
itemCell.setHyperlink(servieLink);//
itemCell.setCellValue(fmtString(record.getProName()));
itemCell.setCellStyle(urlStyle);
itemCell = row.createCell(cellIndex++);
itemCell.setCellValue(record.getServicePrice());
itemCell.setCellStyle(cellStyle);
//
String serviceType = record.getServiceType();
String[] serviceTypes = serviceType.split("-");
for (int jj = 0; jj < serviceTypes.length; jj++) {
if (jj>=3 ) {
break;
}
itemCell = row.createCell(cellIndex++);
itemCell.setCellValue(fmtString(serviceTypes[jj]));
itemCell.setCellStyle(cellStyle);
}
if (serviceTypes.length < 3) {
for (int jjj = 0; jjj < (3 - serviceTypes.length); jjj++) {
itemCell = row.createCell(cellIndex++);
itemCell.setCellValue("");
itemCell.setCellStyle(cellStyle);
}
}
//
itemCell = row.createCell(cellIndex++);
XSSFHyperlink shopLink= (XSSFHyperlink) createHelper.createHyperlink(Hyperlink.LINK_URL);
shopLink.setAddress(record.getShopUrl());//
itemCell.setHyperlink(shopLink);//
itemCell.setCellValue(fmtString(record.getSupName()));
itemCell.setCellStyle(urlStyle);
itemCell = row.createCell(cellIndex++);
itemCell.setCellValue(fmtString(record.getProLineName()));
itemCell.setCellStyle(cellStyle);
itemCell = row.createCell(cellIndex++);
itemCell.setCellValue(fmtString(record.getVipName()));
itemCell.setCellStyle(cellStyle);
itemCell = row.createCell(cellIndex++);
itemCell.setCellValue(fmtString(record.getBaseName()));
itemCell.setCellStyle(cellStyle);
itemCell = row.createCell(cellIndex++);
itemCell.setCellValue(fmtString(record.getFullName()));
itemCell.setCellStyle(cellStyle);
itemCell = row.createCell(cellIndex++);
itemCell.setCellValue(fmtString(record.getPersonOrder()));
itemCell.setCellStyle(cellStyle);
itemCell = row.createCell(cellIndex++);
itemCell.setCellValue(fmtString(record.getOrderMark()));
itemCell.setCellStyle(cellStyle);
}
}
private String fmtString(String str) {
if (StringUtils.isBlank(str)) {
return "";
}
return str;
}
/**
*
* @param wb
* @return
*/
private Map createStyles(Workbook wb) {
Map styles = new HashMap();
Font headFont = wb.createFont();
headFont.setFontHeightInPoints((short) 12);
headFont.setColor(IndexedColors.BLACK.getIndex());
Font cellFont = wb.createFont();
cellFont.setFontHeightInPoints((short) 10);
cellFont.setColor(IndexedColors.BLACK.getIndex());
CellStyle headCellStyle = createBorderedStyle(wb);
headCellStyle.setWrapText(true);
headCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
headCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
headCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT
.getIndex());
headCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
headCellStyle.setFont(cellFont);
styles.put("head", headCellStyle);
CellStyle headTitleStyle = createBorderedStyle(wb);
headTitleStyle.setWrapText(true);
headTitleStyle.setAlignment(CellStyle.ALIGN_LEFT);
headTitleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
headTitleStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT
.getIndex());
headTitleStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
headTitleStyle.setFont(headFont);
styles.put("headTitle", headTitleStyle);
CellStyle cellStyle = createBorderedStyle(wb);
cellStyle.setWrapText(true);
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
cellStyle.setFont(cellFont);
styles.put("cell", cellStyle);
CellStyle urlStyle = createBorderedStyle(wb);
Font urlFont = wb.createFont();
urlFont.setColor(IndexedColors.BLUE.getIndex());
urlFont.setUnderline(HSSFFont.U_SINGLE);
urlStyle.setFont(urlFont);
//cellStyle.setWrapText(true);
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
styles.put("urlStyle", urlStyle);
return styles;
}
private CellStyle createBorderedStyle(Workbook wb) {
CellStyle style = wb.createCellStyle();
style.setBorderRight(CellStyle.BORDER_THIN);
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderTop(CellStyle.BORDER_THIN);
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
return style;
}
public void export(HttpServletResponse response) throws IOException {
response.setContentType("application/vnd.ms-excel");
String excelFileName = sheetTitle + ".xlsx";
response.addHeader("Content-Disposition", "attachment;filename="
+ new String(excelFileName.getBytes("GB2312"), "iso8859-1"));
OutputStream ouputStream = response.getOutputStream();
workbook.write(ouputStream);
ouputStream.flush();
ouputStream.close();
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JAVA http 요청 도구 클래스 http - request우리 의 일상적인 업무 에서 우 리 는 제3자 인터페이스 와 자주 상호작용 을 해 야 한다. 우 리 는 http 프로 토 콜 을 사용 하여 상호작용 을 하 는 경우 가 많다. 자바 원생 은 http 에 대한 지원 (...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.