直接贴代码:
package com.jinhui.reportingcentre.baseinfo.typecatalog.controller;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.DVConstraint;
import org.apache.poi.hssf.usermodel.HSSFDataValidation;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.Name;
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.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.jinhui.core.BaseAction;
import com.jinhui.reportingcentre.Constants;
import com.jinhui.reportingcentre.admin.system.service.ISysParamService;
import com.jinhui.reportingcentre.baseinfo.typecatalog.domain.TypeCatalogBean;
import com.jinhui.reportingcentre.baseinfo.typecatalog.service.ITypeCatalogService;
import com.jinhui.reportingcentre.utils.DeleteFileUtil;
import com.jinhui.reportingcentre.utils.HttpSessionUtil;
/**
* 有害信息类型维护
* @author bin
*/
@Controller
@RequestMapping("/typecatalog")
public class TypeAction extends BaseAction {
private static Logger logger = Logger.getLogger(TypeAction.class);
@Autowired
private ITypeCatalogService typeCatalogService;
private String EXCEL_HIDE_SHEET_NAME = "excelhidesheetname";
private String HIDE_SHEET_NAME_BIGTYPE = "bigTypeList";
private HashMap map = new HashMap();//危害小类
public static final String ERROR = "error";
@Autowired
private ISysParamService sysParamService; // 系统参数service
/**
* 修改excel导入模板
*/
@SuppressWarnings("unchecked")
@RequestMapping("/updateExcel")
public String updateExcel(@RequestParam("type") String type,@RequestParam("reporttype") String reporttype,HttpServletRequest request,
HttpServletResponse response) throws IOException {
PrintWriter pw = response.getWriter();
boolean flag = false;
try {
flag = rewriteExcelTempAtEmp("temp/templates/no_excelDome_web.xls",
"temp/templates/匿名批量导入.xls", 3, "C");
if(flag){
pw.print("1");
}else{
pw.print("2");
}
} catch (Exception e) {
e.printStackTrace();
pw.print("2");
} finally {
pw.flush();
pw.close();
return null;
}
}
//private bigList;//危害大类
/**
* 动态生成危害大类的excel模板
*
* @throws IOException
*/
/**
* 动态生成大类小类级联的excel模板
* @param excelStart 未添加级联的原始Excel模板路径
* @param excelStop 生成最终Excel的保存路径
* @param RowNum 生成在Excel的第几列
* @param Rowletter 生成在Excel的列名(A,B,C...)
* @return
* @throws IOException
*/
private boolean rewriteExcelTempAtEmp(String excelStart, String excelStop,
int RowNum, String Rowletter) throws IOException {
boolean flag = false;
Workbook workbook = null;
try {
String[] bigList = bigTypeList();
// 读取默认模板Excel文件
String path = HttpSessionUtil.getRootPath() + excelStart;
workbook = readWorkBook(path);
if (EXCEL_HIDE_SHEET_NAME.equals(workbook
.getSheetIndex(EXCEL_HIDE_SHEET_NAME))) {
workbook.removeSheetAt(workbook
.getSheetIndex(EXCEL_HIDE_SHEET_NAME)); // 删除隐藏表
}
Sheet hideInfoSheet = workbook.createSheet(EXCEL_HIDE_SHEET_NAME);// 隐藏一些信息
// 在隐藏页设置选择信息
// 第二行设置危害大类
Row bigNameRow = hideInfoSheet.createRow(0);
creatRow(bigNameRow, bigList);
// 以下行设置危害小类列表
for (int i = 0; i < map.size(); i++) {
Row smallNameRow = hideInfoSheet.createRow(i + 1);
String[] Stringlist = (String[]) map.get("bigList" + i);
creatRow(smallNameRow, Stringlist);
}
// 名称管理
// 第一行设置大类信息
creatExcelNameList(workbook, HIDE_SHEET_NAME_BIGTYPE, 1,
bigList.length, false);
// 以后动态大小设置大类对应的小类列表
for (int i = 0; i < bigList.length; i++) {
String[] Stringlist = (String[]) map.get("bigList" + i);
creatExcelNameList(workbook, bigList[i], i + 2,
Stringlist.length, true);
}
// 设置隐藏页标志
workbook.setSheetHidden(
workbook.getSheetIndex(EXCEL_HIDE_SHEET_NAME), true);
// 添加数据
setDataValidation(workbook, RowNum, Rowletter);
String newPath = HttpSessionUtil.getRootPath()+excelStop;
/**** 删除旧的模板 ******/
DeleteFileUtil.deleteFile(newPath);
/**** 生成新的模板 ******/
// 由于POI打开读取文件后再保存时bug问题, 只能重新定义一个新的Excel写入数据
createExcel(workbook, newPath);
flag = true;
} catch (Exception e) {
e.printStackTrace();
workbook.close();
flag = false;
}
return flag;
}
/**
* 读取服务器上面的上传的excel文件
*
* @param path
* @return
*/
public static Workbook readWorkBook(String path) {
Workbook wb = null;
try {
wb = WorkbookFactory.create(new File(path));
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return wb;
}
/**
* 创建一列数据
*
* @param currentRow
* @param textList
*/
private static void creatRow(Row currentRow, String[] textList) {
if (textList != null && textList.length > 0) {
int i = 0;
for (String cellValue : textList) {
Cell userNameLableCell = currentRow.createCell(i++);
userNameLableCell.setCellValue(cellValue);
}
}
}
/**
* 添加数据验证选项
*
* @param sheet
*/
public void setDataValidation(Workbook wb, int RowNum, String Rowletter) {
int sheetIndex = wb.getNumberOfSheets();
if (sheetIndex > 0) {
for (int i = 0; i < sheetIndex; i++) {
Sheet sheet = wb.getSheetAt(i);
if (!EXCEL_HIDE_SHEET_NAME.equals(sheet.getSheetName())) {
DataValidation data_validation_list = null;
// 危害大类选项添加验证数据
for (int a = 4; a < 581; a++) {
// data_validation_list =
// getDataValidationByFormula(HIDE_SHEET_NAME_BIGTYPE,a,8);
data_validation_list = getDataValidationByFormula(
HIDE_SHEET_NAME_BIGTYPE, a, RowNum);
sheet.addValidationData(data_validation_list);
// 危害小类选项添加验证数据
// data_validation_list =
// getDataValidationByFormula("INDIRECT(H"+a+")",a,9);
data_validation_list = getDataValidationByFormula(
"INDIRECT($" + Rowletter +"$"+ a + ")", a,
RowNum + 1);
sheet.addValidationData(data_validation_list);
}
}
}
}
}
/**
* 使用已定义的数据源方式设置一个数据验证
*
* @param formulaString
* @param naturalRowIndex
* @param naturalColumnIndex
* @return
*/
private static DataValidation getDataValidationByFormula(
String formulaString, int naturalRowIndex, int naturalColumnIndex) {
// 加载下拉列表内容
DVConstraint constraint = DVConstraint
.createFormulaListConstraint(formulaString);
// 设置数据有效性加载在哪个单元格上。
// 四个参数分别是:起始行、终止行、起始列、终止列
int firstRow = naturalRowIndex - 1;
int lastRow = naturalRowIndex - 1;
int firstCol = naturalColumnIndex - 1;
int lastCol = naturalColumnIndex - 1;
CellRangeAddressList regions = new CellRangeAddressList(firstRow,
lastRow, firstCol, lastCol);
// 数据有效性对象
DataValidation data_validation_list = new HSSFDataValidation(regions,
constraint);
// //设置输入信息提示信息
// data_validation_list.createPromptBox("下拉选择提示","请使用下拉方式选择合适的值!");
// //设置输入错误提示信息
// data_validation_list.createErrorBox("选择错误提示","你输入的值未在备选列表中,请下拉选择合适的值!");
return data_validation_list;
}
/**
* 创建一个名称
*
* @param workbook
*/
private void creatExcelNameList(Workbook workbook, String nameCode,
int order, int size, boolean cascadeFlag) {
Name name;
name = workbook.createName();
name.setNameName(nameCode);
name.setRefersToFormula(EXCEL_HIDE_SHEET_NAME + "!"
+ creatExcelNameList(order, size, cascadeFlag));
}
/**
* 名称数据行列计算表达式
*
* @param workbook
*/
private static String creatExcelNameList(int order, int size,
boolean cascadeFlag) {
char start = 'A';
if (cascadeFlag) {
start = 'B';
if (size <= 25) {
char end = (char) (start + size - 1);
return "$" + start + "$" + order + ":$" + end + "$" + order;
} else {
char endPrefix = 'A';
char endSuffix = 'A';
if ((size - 25) / 26 == 0 || size == 51) {// 26-51之间,包括边界(仅两次字母表计算)
if ((size - 25) % 26 == 0) {// 边界值
endSuffix = (char) ('A' + 25);
} else {
endSuffix = (char) ('A' + (size - 25) % 26 - 1);
}
} else {// 51以上
if ((size - 25) % 26 == 0) {
endSuffix = (char) ('A' + 25);
endPrefix = (char) (endPrefix + (size - 25) / 26 - 1);
} else {
endSuffix = (char) ('A' + (size - 25) % 26 - 1);
endPrefix = (char) (endPrefix + (size - 25) / 26);
}
}
return "$" + start + "$" + order + ":$" + endPrefix + endSuffix
+ "$" + order;
}
} else {
if (size <= 26) {
char end = (char) (start + size - 1);
return "$" + start + "$" + order + ":$" + end + "$" + order;
} else {
char endPrefix = 'A';
char endSuffix = 'A';
if (size % 26 == 0) {
endSuffix = (char) ('A' + 25);
if (size > 52 && size / 26 > 0) {
endPrefix = (char) (endPrefix + size / 26 - 2);
}
} else {
endSuffix = (char) ('A' + size % 26 - 1);
if (size > 52 && size / 26 > 0) {
endPrefix = (char) (endPrefix + size / 26 - 1);
}
}
return "$" + start + "$" + order + ":$" + endPrefix + endSuffix
+ "$" + order;
}
}
}
/**
* 根据路径创建Excel
*
* @author duanqm
* @param workbook
* @param path
*/
public static void createExcel(Workbook workbook, String path) {
System.setProperty("sun.jnu.encoding","utf-8");
FileOutputStream fileOut = null;
try {
fileOut = new FileOutputStream(path);
workbook.write(fileOut);
} catch (Exception e) {
// logger.error("Error create excel: ", e.getMessage());
} finally {
try {
if (fileOut != null) {
fileOut.close();
workbook.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 获取所有可显示的危害大类,组成数组
*
* @return
*/
public String[] bigTypeList() {
String[] List = new String[] {};
TypeCatalogBean bigBean = new TypeCatalogBean();
bigBean.setParentCodeId(Constants.CODEKIND_TYPECATALOG);
bigBean.setIsdisplay(0);
List<TypeCatalogBean> bigList = typeCatalogService.findNoNetwork(bigBean);
//去掉侵权类0105
for (int i = 0; i < bigList.size(); i++) {
TypeCatalogBean bean = bigList.get(i);
if (bean.getItemId().equals("0105")) {
bigList.remove(bean);
break;
}
}
if (bigList.size() > 0) {
String[] blist = new String[bigList.size()];
for (int i = 0; i < bigList.size(); i++) {
blist[i] = bigList.get(i).getItemName();// 危害大类存入数组
// 获取子类
TypeCatalogBean smallBean = new TypeCatalogBean();
smallBean.setParentCodeId(bigList.get(i).getItemId());
List<TypeCatalogBean> smallList = typeCatalogService
.queryForList(smallBean);
if (smallList.size() > 0) {
// 存放子类
String[] slist = new String[smallList.size() + 1];
slist[0] = bigList.get(i).getItemName();// 危害大类先放进去
for (int j = 0; j < smallList.size(); j++) {
slist[j + 1] = smallList.get(j).getItemName();// 危害子类存入数组
}
map.put("bigList" + i, slist);
} else {
String[] slist = new String[1];
slist[0] = bigList.get(i).getItemName();// 危害大类先放进去
map.put("bigList" + i, slist);
}
}
List = blist;
}
return List;
}
}