poi导出xlsx格式的excel怎么打不开,求大神帮忙!急
代码如下
/**
* 执行导出Excel操作
*/
function opExportOne(e) {
var f = KaTeX parse error: Expected 'EOF', got '#' at position 3: ("#̲requirePlanDeta…(e.currentTarget).closest(“tr”));
var url = “${ctx}/oms/requirePlan/1/” + false+"/exportDetail?ids="+f.id;
window.location.href = url;
}
/**
* Excel导出
* @param ids
* @param templateId
* @param isTransient
* @param request
* @param response
*/
@RequestMapping(value = “{templateId}/{isTransient}/exportDetail”)
public void exportExcel(@RequestParam(value = “ids”,required = false) String[] ids,
@PathVariable(“templateId”) String templateId,
@PathVariable(“isTransient”) boolean isTransient,
HttpServletRequest request, HttpServletResponse response) {
Searchable searchable = parseSearchFromExport(request);
requirePlanDetailService.exportExcel(ids, searchable, response, templateId, isTransient);
}
业务层:
@Override
public void exportExcel(String[] ids,Searchable searchable, HttpServletResponse response, String templateId, boolean isTransient) {
searchable.removePageable();
UserAuthModel userAuthModel = (UserAuthModel) UserInfoUtil.getCurrentLoginUser();
List purchaseGroupIdList = userAuthModel.getPurchaseGroupIds();
if (CollectionUtil.isNotEmpty(purchaseGroupIdList)) {
searchable.addSearchFilter(“groupId”, Operator.IN, purchaseGroupIdList);
}
List<String> storerGroupIds = userAuthModel.getStorerGroupIds();
if (CollectionUtil.isNotEmpty(storerGroupIds)) {
searchable.addSearchFilter("parentObj.customerId", Operator.IN, storerGroupIds);
}
String[] rowName = {"序号","计划号","计划行号","预留号","提报单元","物料编码","物料描述","供应商","采购订单号","主单位","需求数量","需求日期",
"采购申请号","WBS元素","网络号","工单号","工单描述","项目、装置名称","施工单位"};
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("需求计划明细表");
HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);
HSSFCellStyle style = this.getStyle(workbook);
// 定义所需列数
int columnNum = rowName.length;
HSSFRow rowRowName = sheet.createRow(1);
rowRowName.setHeight((short) (25 * 25));
// 将列头设置到sheet的单元格中
for(int n=0;n<columnNum;n++){
HSSFCell cellRowName = rowRowName.createCell(n); //创建列头对应个数的单元格
cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); //设置列头单元格的数据类型
HSSFRichTextString text = new HSSFRichTextString(rowName[n]);
cellRowName.setCellValue(text); //设置列头单元格的值
cellRowName.setCellStyle(columnTopStyle); //设置列头单元格样式
}
//写入数据
List<RequirePlanDetail> result = requirePlanDetailRepository.findAll(Arrays.asList(ids));
int i = 1;
for(RequirePlanDetail requirePlanDetail : result){
if(requirePlanDetail.getIsResease().equals(YesNoEnum.YES.getCode())){
continue;
}
HSSFRow row = sheet.createRow(++i);
row.setHeight((short) (30 * 20));
HSSFCell cell0 = row.createCell(0);
cell0.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell0.setCellValue(i - 1);
cell0.setCellStyle(style);
this.setStyleAndValue(row.createCell(1), requirePlanDetail.getPlanNo(), style);
this.setStyleAndValue(row.createCell(2),requirePlanDetail.getColNo(),style);
this.setStyleAndValue(row.createCell(3),requirePlanDetail.getReserveNo(),style);
this.setStyleAndValue(row.createCell(4), requirePlanDetail.getParentObj().getCustomerName(), style);
this.setStyleAndValue(row.createCell(5),requirePlanDetail.getPartNo(),style);
this.setStyleAndValue(row.createCell(6),requirePlanDetail.getPartName(),style);
this.setStyleAndValue(row.createCell(7),requirePlanDetail.getVendorName(),style);
this.setStyleAndValue(row.createCell(8),requirePlanDetail.getPoNo(),style);
this.setStyleAndValue(row.createCell(9),requirePlanDetail.getUnitName(),style);
this.setStyleAndValue(row.createCell(10),String.valueOf(requirePlanDetail.getQtyRequire()),style);
this.setStyleAndValue(row.createCell(11),requirePlanDetail.getParentObj().getRequireDate() == null ? "" : DateUtil.formatDate(requirePlanDetail.getParentObj().getRequireDate(), "yyyy-MM-dd HH:mm:ss"),style);
this.setStyleAndValue(row.createCell(12),requirePlanDetail.getApplyNo(),style);
this.setStyleAndValue(row.createCell(13),requirePlanDetail.getWbsElement(),style);
this.setStyleAndValue(row.createCell(14),requirePlanDetail.getNetworkNo(),style);
this.setStyleAndValue(row.createCell(15),requirePlanDetail.getJobNo(),style);
this.setStyleAndValue(row.createCell(16),requirePlanDetail.getJobDesc(),style);
this.setStyleAndValue(row.createCell(17),requirePlanDetail.getProjectName(),style);
this.setStyleAndValue(row.createCell(18),requirePlanDetail.getBuilderName(),style);
}
//让列宽随着导出的列长自动适应
for (int colNum = 0; colNum <=18; colNum++) {
int columnWidth = sheet.getColumnWidth(colNum) / 256;
for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
HSSFRow currentRow;
//当前行未被使用过
if (sheet.getRow(rowNum) == null) {
currentRow = sheet.createRow(rowNum);
} else {
currentRow = sheet.getRow(rowNum);
}
if (currentRow.getCell(colNum) != null) {
HSSFCell currentCell = currentRow.getCell(colNum);
if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
int length = currentCell.getStringCellValue().getBytes().length;
if (columnWidth < length) {
columnWidth = length;
}
}
}
}
if(colNum == 0){
sheet.setColumnWidth(colNum, (columnWidth-2) * 128);
}else{
sheet.setColumnWidth(colNum, (columnWidth+4) * 256);
}
}
try {
String fileName = "需求计划明细";
//输出Excel文件
OutputStream output=response.getOutputStream();
response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode(fileName + DateUtil.formatDate(new Date(), "yyyyMMdd") + ".xlsx", "UTF-8"))));
response.setHeader("Connection", "close");
response.setHeader("Content-Type", "application/vnd.ms-excel");
workbook.write(output);
output.close();
}catch(Exception e){
LogUtil.logError("导出Excel失败!原因:" + e.getMessage());
}
}
/*
* 列头单元格样式
*/
public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) {
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short) 12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyle style = workbook.createCellStyle();
style.setFont(font);
style.setWrapText(false);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style.setFillForegroundColor(IndexedColors.WHITE.getIndex());
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
return style;
}
/*
* 列数据信息头单元格样式
*/
public HSSFCellStyle getStyle(HSSFWorkbook workbook) {
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short) 12);
HSSFCellStyle style = workbook.createCellStyle();
style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
style.setFont(font);
style.setWrapText(false);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
return style;
}
/*
* 列数据信息单元格样式
* @param cell
* @param value
* @param style
*/
public void setStyleAndValue(HSSFCell cell,String value,HSSFCellStyle style){
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
HSSFRichTextString text = new HSSFRichTextString(value);
cell.setCellValue(text);
cell.setCellStyle(style);
}
链接: link.
图片:
带尺寸的图片:
居中的图片:
居中并且带尺寸的图片:
当然,我们为了让用户更加便捷,我们增加了图片拖拽功能。
去博客设置页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片
.
// An highlighted block
var foo = 'bar';
一个简单的表格是这么创建的:
项目 | Value |
---|---|
电脑 | $1600 |
手机 | $12 |
导管 | $1 |
使用:---------:
居中
使用:----------
居左
使用----------:
居右
第一列 | 第二列 | 第三列 |
---|---|---|
第一列文本居中 | 第二列文本居右 | 第三列文本居左 |
SmartyPants将ASCII标点字符转换为“智能”印刷标点HTML实体。例如:
TYPE | ASCII | HTML |
---|---|---|
Single backticks | 'Isn't this fun?' | ‘Isn’t this fun?’ |
Quotes | "Isn't this fun?" | “Isn’t this fun?” |
Dashes | -- is en-dash, --- is em-dash | – is en-dash, — is em-dash |
一个具有注脚的文本。1
Markdown将文本转换为 HTML。
您可以使用渲染LaTeX数学表达式 KaTeX:
Gamma公式展示 Γ ( n ) = ( n − 1 ) ! ∀ n ∈ N \Gamma(n) = (n-1)!\quad\forall n\in\mathbb N Γ(n)=(n−1)!∀n∈N 是通过欧拉积分
Γ ( z ) = ∫ 0 ∞ t z − 1 e − t d t   . \Gamma(z) = \int_0^\infty t^{z-1}e^{-t}dt\,. Γ(z)=∫0∞tz−1e−tdt.
你可以找到更多关于的信息 LaTeX 数学表达式here.