摘要:四个参数分别是起始行终止行起始列终止列数据有效性对象包下载百度云盘外链
xml文件
execel的行和列以0开头 设置单元格居中</>复制代码
<span class="hljs-tag"></<span class="hljs-name">li</span>></span><span class="hljs-tag"><<span class="hljs-name">li</span>></span> <span class="hljs-tag"><<span class="hljs-name">tr</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"16px"</span>></span><span class="hljs-tag"></<span class="hljs-name">li</span>></span><span class="hljs-tag"><<span class="hljs-name">li</span>></span> <span class="hljs-tag"><<span class="hljs-name">td</span> <span class="hljs-attr">rowspan</span>=<span class="hljs-string">"1"</span> <span class="hljs-attr">colspan</span>=<span class="hljs-string">"6"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"学生信息导入"</span> /></span><span class="hljs-tag"></<span class="hljs-name">li</span>></span><span class="hljs-tag"><<span class="hljs-name">li</span>></span> <span class="hljs-tag"></<span class="hljs-name">tr</span>></span><span class="hljs-tag"></<span class="hljs-name">li</span>></span><span class="hljs-tag"><<span class="hljs-name">li</span>></span>
HSSFCellStyle cellStyle = wb.createCellStyle();//创建单元格样式
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//设置单元格对齐方式
</>复制代码
HSSFFont font = wb.createFont();
font.setFontName("仿宋_GB2312");
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//字体加粗
//font.setFontHeight((short)12);
font.setFontHeightInPoints((short)12);
cellStyle.setFont(font);
cell.setCellStyle(cellStyle);
//合并单元格居中
</>复制代码
sheet.addMergedRegion(new CellRangeAddress(rspan, rspan, 0, cspan));
设置单元格数据类型
</>复制代码
/**
* 测试单元格样式
* @author David
* @param wb
* @param cell
* @param td
*/
private static void setType(HSSFWorkbook wb, HSSFCell cell, Element td) {
Attribute typeAttr = td.getAttribute("type");
String type = typeAttr.getValue();
HSSFDataFormat format = wb.createDataFormat();
HSSFCellStyle cellStyle = wb.createCellStyle();
if("NUMERIC".equalsIgnoreCase(type)){
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
Attribute formatAttr = td.getAttribute("format");
String formatValue = formatAttr.getValue();
formatValue = StringUtils.isNotBlank(formatValue)? formatValue : "#,##0.00";
cellStyle.setDataFormat(format.getFormat(formatValue));
}else if("STRING".equalsIgnoreCase(type)){
cell.setCellValue("");
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cellStyle.setDataFormat(format.getFormat("@"));
}else if("DATE".equalsIgnoreCase(type)){
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cellStyle.setDataFormat(format.getFormat("yyyy-m-d"));
}else if("ENUM".equalsIgnoreCase(type)){
CellRangeAddressList regions =
new CellRangeAddressList(cell.getRowIndex(), cell.getRowIndex(),
cell.getColumnIndex(), cell.getColumnIndex());
Attribute enumAttr = td.getAttribute("format");
String enumValue = enumAttr.getValue();
//加载下拉列表内容
DVConstraint constraint =
DVConstraint.createExplicitListConstraint(enumValue.split(","));
//数据有效性对象
HSSFDataValidation dataValidation = new HSSFDataValidation(regions, constraint);
wb.getSheetAt(0).addValidationData(dataValidation);
}
cell.setCellStyle(cellStyle);
}
设置下拉列表类型
封装设置数据有效性方法
</>复制代码
/**
* 方法名称:SetDataValidation
* 内容摘要:设置数据有效性
* @param sheet excel sheet內容
* @param textList 下拉列表
* @param firstRow 單元格範圍
* @param firstCol
* @param endRow
* @param endCol
*/
private static HSSFDataValidation setDataValidation(HSSFSheet sheet,String[] textList,short firstRow,short firstCol, short endRow, short endCol) {
//加载下拉列表内容
DVConstraint constraint = DVConstraint.createExplicitListConstraint(textList);
//设置数据有效性加载在哪个单元格上。
//四个参数分别是:起始行、终止行、起始列、终止列
CellRangeAddressList regions = new CellRangeAddressList(firstRow,endRow, firstCol, endCol);
//数据有效性对象
HSSFDataValidation data_validation = new HSSFDataValidation(regions, constraint);
sheet.addValidationData(data_validation);
return data_validation;
}
设置列宽方法封装
</>复制代码
/**
* 设置列宽
* @author David
* @param sheet
* @param colgroup
*/
private static void setColumnWidth(HSSFSheet sheet, Element colgroup) {
List cols = colgroup.getChildren("col");
for (int i = 0; i < cols.size(); i++) {
Element col = cols.get(i);
Attribute width = col.getAttribute("width");
String unit = width.getValue().replaceAll("[0-9,.]", "");//截取单位
String value = width.getValue().replaceAll(unit, "");//擦除单位
int v=0;
//单位转化
if(StringUtils.isBlank(unit) || "px".endsWith(unit)){//如果单位为空或等于px
v = Math.round(Float.parseFloat(value) * 37F);
}else if ("em".endsWith(unit)){//如果单位为em
v = Math.round(Float.parseFloat(value) * 267.5F);
}
sheet.setColumnWidth(i, v);//设置第i列宽度为v
}
}
完整代码
</>复制代码
package com.imooc.excel;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
/**
* Created by chenld1 on 2015/10/6.
*/
public class CreateTemplate {
/**
* 创建模板文件
* @author David
* @param args
*/
public static void main(String[] args) {
//获取解析xml文件路径
String path = System.getProperty("user.dir") + "/student2.xml";
File file = new File(path);
SAXBuilder builder = new SAXBuilder();
try {
//解析xml文件
Document parse = builder.build(file);
//创建Excel
HSSFWorkbook wb = new HSSFWorkbook();
//创建sheet
HSSFSheet sheet = wb.createSheet("Sheet0");
//获取xml文件跟节点
Element root = parse.getRootElement();
//获取模板名称
String templateName = root.getAttribute("name").getValue();
int rownum = 0;
int column = 0;
//设置列宽
Element colgroup = root.getChild("colgroup");
setColumnWidth(sheet,colgroup);
//设置标题
Element title = root.getChild("title");
List trs = title.getChildren("tr");
for (int i = 0; i < trs.size(); i++) {
Element tr = trs.get(i);
List tds = tr.getChildren("td");
HSSFRow row = sheet.createRow(rownum);
HSSFCellStyle cellStyle = wb.createCellStyle();//创建单元格样式
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//设置单元格对齐方式
for(column = 0;column ths = tr.getChildren("th");
for(column = 0;column < ths.size();column++){
Element th = ths.get(column);
Attribute valueAttr = th.getAttribute("value");
HSSFCell cell = row.createCell(column);
if(valueAttr != null){
String value =valueAttr.getValue();
cell.setCellValue(value);
}
}
rownum++;
}
//设置数据区域样式
Element tbody = root.getChild("tbody");
Element tr = tbody.getChild("tr");
int repeat = tr.getAttribute("repeat").getIntValue();
List tds = tr.getChildren("td");
for (int i = 0; i < repeat; i++) {
HSSFRow row = sheet.createRow(rownum);
for(column =0 ;column < tds.size();column++){
Element td = tds.get(column);
HSSFCell cell = row.createCell(column);
setType(wb,cell,td);
}
rownum++;
}
//生成Excel导入模板
File tempFile = new File("e:/" + templateName + ".xls");
tempFile.delete();
tempFile.createNewFile();
FileOutputStream stream = FileUtils.openOutputStream(tempFile);
wb.write(stream);
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 设置单元格数据类型
* @author David
* @param wb
* @param cell
* @param td
*/
private static void setType(HSSFWorkbook wb, HSSFCell cell, Element td) {
Attribute typeAttr = td.getAttribute("type");
String type = typeAttr.getValue();
//HSSFDataformat
HSSFDataFormat format = wb.createDataFormat();
HSSFCellStyle cellStyle = wb.createCellStyle();
if("NUMERIC".equalsIgnoreCase(type)){
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
Attribute formatAttr = td.getAttribute("format");
String formatValue = formatAttr.getValue();
formatValue = StringUtils.isNotBlank(formatValue)? formatValue : "#,##0.00";
cellStyle.setDataFormat(format.getFormat(formatValue));
}else if("STRING".equalsIgnoreCase(type)){
cell.setCellValue("");
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cellStyle.setDataFormat(format.getFormat("@"));
}else if("DATE".equalsIgnoreCase(type)){
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cellStyle.setDataFormat(format.getFormat("yyyy-m-d"));
}else if("ENUM".equalsIgnoreCase(type)){
CellRangeAddressList regions =
new CellRangeAddressList(cell.getRowIndex(), cell.getRowIndex(),
cell.getColumnIndex(), cell.getColumnIndex());
Attribute enumAttr = td.getAttribute("format");
String enumValue = enumAttr.getValue();
//加载下拉列表内容
DVConstraint constraint =
DVConstraint.createExplicitListConstraint(enumValue.split(","));
//数据有效性对象
HSSFDataValidation dataValidation = new HSSFDataValidation(regions, constraint);
wb.getSheetAt(0).addValidationData(dataValidation);
}
cell.setCellStyle(cellStyle);
}
/**
* 设置列宽
* @author David
* @param sheet
* @param colgroup
*/
private static void setColumnWidth(HSSFSheet sheet, Element colgroup) {
List cols = colgroup.getChildren("col");
for (int i = 0; i < cols.size(); i++) {
Element col = cols.get(i);
Attribute width = col.getAttribute("width");
String unit = width.getValue().replaceAll("[0-9,.]", "");//截取单位
String value = width.getValue().replaceAll(unit, "");//擦除单位
int v=0;
//单位转化
if(StringUtils.isBlank(unit) || "px".endsWith(unit)){//如果单位为空或等于px
v = Math.round(Float.parseFloat(value) * 37F);
}else if ("em".endsWith(unit)){//如果单位为em
v = Math.round(Float.parseFloat(value) * 267.5F);
}
sheet.setColumnWidth(i, v);//设置第i列宽度为v
}
}
/**
* 方法名称:SetDataValidation
* 内容摘要:设置数据有效性
* @param sheet excel sheet內容
* @param textList 下拉列表
* @param firstRow 單元格範圍
* @param firstCol
* @param endRow
* @param endCol
*/
private static HSSFDataValidation setDataValidation(HSSFSheet sheet,String[] textList,short firstRow,short firstCol, short endRow, short endCol) {
//加载下拉列表内容
DVConstraint constraint = DVConstraint.createExplicitListConstraint(textList);
//设置数据有效性加载在哪个单元格上。
//四个参数分别是:起始行、终止行、起始列、终止列
CellRangeAddressList regions = new CellRangeAddressList(firstRow,endRow, firstCol, endCol);
//数据有效性对象
HSSFDataValidation data_validation = new HSSFDataValidation(regions, constraint);
sheet.addValidationData(data_validation);
return data_validation;
}
}
jar包下载
百度云盘外链
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/64637.html
摘要:需要的技术框架利用其上传下载功能解析技术定制导入模板制作前台与格式对应,版本低,兼容性好与格式对应组成的几个概念工作薄工作表行记录单元格创建中的的详见如创建创建工作簿创建工作表创建第一行创建一个文件存盘名字性別男解析文件创建,读取文件 需要的技术 1、strut2框架 利用其上传下载功能2、xml解析技术 定制导入模板3、jquery UI 制作前台 4、showImg(/i...
时间:2017年07月06日星期四说明:本文部分内容均来自慕课网。@慕课网:http://www.imooc.com教学源码:无学习源码:https://github.com/zccodere/s... 第一章:课程介绍 1-1 预备知识 基础知识 struts2框架(上传下载功能) xml解析技术(导入模板) JQuery EasyUI(前台美观) 课程目录 实现方式 定制导入模版 导入文件 导...
摘要:我想能不能像配置文件一样可配置的导入导出,那样使用起来就方便许多。配置和使用下面是员工信息模型。支持多种映射,使用英文逗号进行分割。导入时它会以分割前面的作为导入时使用的值,后面的作为导出时使用的值后面值进行逆推导出时同理。 1.前言 在工作时,遇到过这样的需求,需要灵活的对工单进行导入或导出,以前自己也做过,但使用不灵活繁琐。我想能不能像配置文件一样可配置的导入导出,那样使用起来就方...
摘要:复制粘贴单元格格式和单元格类型本周,让我们一起来学习的复制粘贴单元格格式和单元格类型,希望我的学习笔记能够帮助你们,从零开始学习,并逐步精通。 复制粘贴、单元格格式和单元格类型 本周,让我们一起来学习SpreadJS 的复制粘贴、单元格格式和单元格类型,希望我的学习笔记能够帮助你们,从零开始学习 SpreadJS,并逐步精通。 在此前的学习笔记中,相信大家已经学会并熟练掌握了Sprea...
阅读 3717·2021-11-15 11:38
阅读 2868·2021-11-11 16:55
阅读 2670·2021-11-08 13:22
阅读 2883·2021-11-02 14:45
阅读 1420·2021-09-28 09:35
阅读 2855·2021-09-10 10:50
阅读 547·2019-08-30 15:44
阅读 2925·2019-08-29 17:06