controller
@RequestMapping("assign2/downloadAssignWayConfigHistorydo")public void downloadAssignWayConfigHistory(>}
service
public void exportAssignCaseWayHistory(String[] exceltitles,ArrayList<Map<String, Object>> excelDatas, OutputStream out) {
try{
// 第一步,创建一个workbook,对应一个Excel文件
HSSFWorkbook workbook = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet hssfSheet = workbookcreateSheet("sheet1");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = hssfSheetcreateRow(0);
//不带背景颜色居中的cellStyle
HSSFCellStyle hssfCellStyle = workbookcreateCellStyle();
//居中
hssfCellStylesetAlignment(HSSFCellStyleALIGN_CENTER);
hssfCellStylesetVerticalAlignment(hssfCellStyleVERTICAL_CENTER);
//带背景和字体颜色并且居中的cellStyle
HSSFCellStyle hssfCellStyleWithBackgroundColor = workbookcreateCellStyle();
//居中
hssfCellStyleWithBackgroundColorsetAlignment(HSSFCellStyleALIGN_CENTER);
hssfCellStyleWithBackgroundColorsetVerticalAlignment(hssfCellStyleVERTICAL_CENTER);
//背景
hssfCellStyleWithBackgroundColorsetFillForegroundColor(IndexedColorsYELLOWgetIndex());
hssfCellStyleWithBackgroundColorsetFillPattern(hssfCellStyleSOLID_FOREGROUND);
//字体
HSSFFont font = workbookcreateFont();
fontsetColor(HSSFColorREDindex);
hssfCellStyleWithBackgroundColorsetFont(font);
HSSFCell hssfCell = null;
for (int i = 0; i < exceltitleslength; i++) {
hssfCell = rowcreateCell(i);//列索引从0开始
hssfCellsetCellValue(exceltitles[i]);//列名1
hssfCellsetCellStyle(hssfCellStyle);//列居中显示
//设置列宽
if (i == 1 || i == 6 || i == 7) {
hssfSheetsetColumnWidth(i, 5000);
}else {
hssfSheetsetColumnWidth(i, 3000);
}
}
// 第五步,写入实体数据
for (int i = 0; i < excelDatassize(); i++) {
row = hssfSheetcreateRow(2i+1); //奇数行
Map<String, Object> excelData = excelDatasget(i);
// 第六步,创建单元格,并设置值
String updateBy = (String) excelDataget("updateBy");
String updateDate = (String) excelDataget("updateDate");
String region = (String) excelDataget("region");
String branch = (String) excelDataget("branch");
String center = (String) excelDataget("center");
String dept = (String) excelDataget("dept");
String assignMode = (String) excelDataget("assignMode");
String newAssignMode = (String) excelDataget("newAssignMode");
String turnableDimension = (String) excelDataget("turnableDimension");
String newTurnableDimension = (String) excelDataget("newTurnableDimension");
String M0 = (String) excelDataget("M0");
String M1 = (String) excelDataget("M1");
String M2 = (String) excelDataget("M2");
String M3 = (String) excelDataget("M3");
String newM0 = (String) excelDataget("newM0");
String newM1 = (String) excelDataget("newM1");
String newM2 = (String) excelDataget("newM2");
String newM3 = (String) excelDataget("newM3");
HSSFCell cell0 = rowcreateCell(0);
cell0setCellStyle(hssfCellStyle);
cell0setCellValue(updateBy);
HSSFCell cell1 = rowcreateCell(1);
cell1setCellStyle(hssfCellStyle);
cell1setCellValue(updateDate);
HSSFCell cell2 = rowcreateCell(2);
cell2setCellStyle(hssfCellStyle);
cell2setCellValue(region);
HSSFCell cell3 = rowcreateCell(3);
cell3setCellStyle(hssfCellStyle);
cell3setCellValue(branch);
HSSFCell cell4 = rowcreateCell(4);
cell4setCellStyle(hssfCellStyle);
cell4setCellValue(center);
HSSFCell cell5 = rowcreateCell(5);
cell5setCellStyle(hssfCellStyle);
cell5setCellValue(dept);
HSSFCell cell6 = rowcreateCell(6);
cell6setCellStyle(hssfCellStyle);
cell6setCellValue(assignModeequals("0")"模式1":"模式2");
if ("BRANCH"equals(turnableDimension)) {
turnableDimension = "分部";
} else if ("CENTER"equals(turnableDimension)) {
turnableDimension = "中心";
} else if ("DEPT"equals(turnableDimension)) {
turnableDimension = "部";
}
HSSFCell cell7 = rowcreateCell(7);
cell7setCellStyle(hssfCellStyle);
cell7setCellValue(turnableDimension);
HSSFCell cell8 = rowcreateCell(8);
cell8setCellStyle(hssfCellStyle);
cell8setCellValue(M0equalsIgnoreCase("Y")"案2":"案2");
HSSFCell cell9 = rowcreateCell(9);
cell9setCellStyle(hssfCellStyle);
cell9setCellValue(M1equalsIgnoreCase("Y")"案1":"案2");
HSSFCell cell10 = rowcreateCell(10);
cell10setCellStyle(hssfCellStyle);
cell10setCellValue(M2equalsIgnoreCase("Y")"案1":"案2");
HSSFCell cell11 = rowcreateCell(11);
cell11setCellStyle(hssfCellStyle);
cell11setCellValue(M3equalsIgnoreCase("Y")"案1":"案2");
row = hssfSheetcreateRow(2i+2); //偶数行
HSSFCell newCell6 = rowcreateCell(6);
newCell6setCellValue(newAssignModeequals("0")"模式1":"模式2");//设置值
if (!newAssignModeequals(assignMode)){//设置背景色
newCell6setCellStyle(hssfCellStyleWithBackgroundColor);
}else {
newCell6setCellStyle(hssfCellStyle);
}
if ("BRANCH"equals(newTurnableDimension)) {
newTurnableDimension = "haha";
} else if ("CENTER"equals(newTurnableDimension)) {
newTurnableDimension = "hehe";
} else if ("DEPT"equals(newTurnableDimension)) {
newTurnableDimension = "hiahia";
}
HSSFCell newCell7 = rowcreateCell(7);
newCell7setCellValue(newTurnableDimension);
if (!newTurnableDimensionequals(turnableDimension)){//设置背景色
newCell7setCellStyle(hssfCellStyleWithBackgroundColor);
}else {
newCell7setCellStyle(hssfCellStyle);
}
HSSFCell newCell8 = rowcreateCell(8);
newCell8setCellValue(newM0equalsIgnoreCase("Y")"案1":"案2");
if (!newM0equals(M0)){//设置背景色
newCell8setCellStyle(hssfCellStyleWithBackgroundColor);
}else {
newCell8setCellStyle(hssfCellStyle);
}
HSSFCell newCell9 = rowcreateCell(9);
newCell9setCellValue(newM1equalsIgnoreCase("Y")"案1":"案2");
if (!newM1equals(M1)){//设置背景色
newCell9setCellStyle(hssfCellStyleWithBackgroundColor);
}else {
newCell9setCellStyle(hssfCellStyle);
}
HSSFCell newCell10 = rowcreateCell(10);
newCell10setCellValue(newM2equalsIgnoreCase("Y")"案1":"案2");
if (!newM2equals(M2)){//设置背景色
newCell10setCellStyle(hssfCellStyleWithBackgroundColor);
}else {
newCell10setCellStyle(hssfCellStyle);
}
HSSFCell newCell11 = rowcreateCell(11);
newCell11setCellValue(newM3equalsIgnoreCase("Y")"案1":"案2");
if (!newM3equals(M3)){//设置背景色
newCell11setCellStyle(hssfCellStyleWithBackgroundColor);
}else {
newCell11setCellStyle(hssfCellStyle);
}
//合并单元格
for (int j = 0; j < 6; j++){
CellRangeAddress cellRangeAddress = new CellRangeAddress(2i+1, 2i+2, j, j);
hssfSheetaddMergedRegion(cellRangeAddress);
}
}
// 第七步,将文件输出到客户端浏览器
try {
workbookwrite(out);
outflush();
outclose();
} catch (Exception e) {
eprintStackTrace();
}
}catch(Exception e){
eprintStackTrace();
}
}
package edusjtuerplabpoi;
import javaioInputStream&ch=wwxqychain" target="_blank" class="link-baike">FileInputStream;
import javaioFileNotFoundException;
import javaioIOException;
import javaioInputStream;
import javatextSimpleDateFormat;
import javautilDate;
import javautilHashMap;
import javautilMap;
import orgapachepoihssfusermodelHSSFCell;
import orgapachepoihssfusermodelHSSFDateUtil;
import orgapachepoihssfusermodelHSSFRow;
import orgapachepoihssfusermodelHSSFSheet;
import orgapachepoihssfusermodelHSSFWorkbook;
import orgapachepoipoifsfilesystemPOIFSFileSystem;
/
*** 作Excel表格的功能类
/
public class ExcelReader {
private POIFSFileSystem fs;
private HSSFWorkbook wb;
private HSSFSheet sheet;
private HSSFRow row;
/
读取Excel表格表头的内容
@param InputStream
@return String 表头内容的数组
/
public String[] readExcelTitle(InputStream is) {
try {
fs = new POIFSFileSystem(is);
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
eprintStackTrace();
}
sheet = wbgetSheetAt(0);
row = sheetgetRow(0);
// 标题总列数
int colNum = rowgetPhysicalNumberOfCells();
Systemoutprintln("colNum:" + colNum);
String[] title = new String[colNum];
for (int i = 0; i < colNum; i++) {
//title[i] = getStringCellValue(rowgetCell((short) i));
title[i] = getCellFormatValue(rowgetCell((short) i));
}
return title;
}
/
读取Excel数据内容
@param InputStream
@return Map 包含单元格数据内容的Map对象
/
public Map<Integer, String> readExcelContent(InputStream is) {
Map<Integer, String> content = new HashMap<Integer, String>();
String str = "";
try {
fs = new POIFSFileSystem(is);
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
eprintStackTrace();
}
sheet = wbgetSheetAt(0);
// 得到总行数
int rowNum = sheetgetLastRowNum();
row = sheetgetRow(0);
int colNum = rowgetPhysicalNumberOfCells();
// 正文内容应该从第二行开始,第一行为表头的标题
for (int i = 1; i <= rowNum; i++) {
row = sheetgetRow(i);
int j = 0;
while (j < colNum) {
// 每个单元格的数据内容用"-"分割开,以后需要时用String类的replace()方法还原数据
// 也可以将每个单元格的数据设置到一个javabean的属性中,此时需要新建一个javabean
// str += getStringCellValue(rowgetCell((short) j))trim() +
// "-";
str += getCellFormatValue(rowgetCell((short) j))trim() + " ";
j++;
}
contentput(i, str);
str = "";
}
return content;
}
/
获取单元格数据内容为字符串类型的数据
@param cell Excel单元格
@return String 单元格数据内容
/
private String getStringCellValue(HSSFCell cell) {
String strCell = "";
switch (cellgetCellType()) {
case HSSFCellCELL_TYPE_STRING:
strCell = cellgetStringCellValue();
break;
case HSSFCellCELL_TYPE_NUMERIC:
strCell = StringvalueOf(cellgetNumericCellValue());
break;
case HSSFCellCELL_TYPE_BOOLEAN:
strCell = StringvalueOf(cellgetBooleanCellValue());
break;
case HSSFCellCELL_TYPE_BLANK:
strCell = "";
break;
default:
strCell = "";
break;
}
if (strCellequals("") || strCell == null) {
return "";
}
if (cell == null) {
return "";
}
return strCell;
}
/
获取单元格数据内容为日期类型的数据
@param cell
Excel单元格
@return String 单元格数据内容
/
private String getDateCellValue(HSSFCell cell) {
String result = "";
try {
int cellType = cellgetCellType();
if (cellType == HSSFCellCELL_TYPE_NUMERIC) {
Date date = cellgetDateCellValue();
result = (dategetYear() + 1900) + "-" + (dategetMonth() + 1)
+ "-" + dategetDate();
} else if (cellType == HSSFCellCELL_TYPE_STRING) {
String date = getStringCellValue(cell);
result = datereplaceAll("[年月]", "-")replace("日", "")trim();
} else if (cellType == HSSFCellCELL_TYPE_BLANK) {
result = "";
}
} catch (Exception e) {
Systemoutprintln("日期格式不正确!");
eprintStackTrace();
}
return result;
}
/
根据HSSFCell类型设置数据
@param cell
@return
/
private String getCellFormatValue(HSSFCell cell) {
String cellvalue = "";
if (cell != null) {
// 判断当前Cell的Type
switch (cellgetCellType()) {
// 如果当前Cell的Type为NUMERIC
case HSSFCellCELL_TYPE_NUMERIC:
case HSSFCellCELL_TYPE_FORMULA: {
// 判断当前的cell是否为Date
if (HSSFDateUtilisCellDateFormatted(cell)) {
// 如果是Date类型则,转化为Data格式
//方法1:这样子的data格式是带时分秒的:2011-10-12 0:00:00
//cellvalue = cellgetDateCellValue()toLocaleString();
//方法2:这样子的data格式是不带带时分秒的:2011-10-12
Date date = cellgetDateCellValue();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
cellvalue = sdfformat(date);
}
// 如果是纯数字
else {
// 取得当前Cell的数值
cellvalue = StringvalueOf(cellgetNumericCellValue());
}
break;
}
// 如果当前Cell的Type为STRIN
case HSSFCellCELL_TYPE_STRING:
// 取得当前的Cell字符串
cellvalue = cellgetRichStringCellValue()getString();
break;
// 默认的Cell值
default:
cellvalue = " ";
}
} else {
cellvalue = "";
}
return cellvalue;
}
public static void main(String[] args) {
try {
// 对读取Excel表格标题测试
InputStream is = new FileInputStream("d:\\test2xls");
ExcelReader excelReader = new ExcelReader();
String[] title = excelReaderreadExcelTitle(is);
Systemoutprintln("获得Excel表格的标题:");
for (String s : title) {
Systemoutprint(s + " ");
}
// 对读取Excel表格内容测试
InputStream is2 = new FileInputStream("d:\\test2xls");
Map<Integer, String> map = excelReaderreadExcelContent(is2);
Systemoutprintln("获得Excel表格的内容:");
for (int i = 1; i <= mapsize(); i++) {
Systemoutprintln(mapget(i));
}
} catch (FileNotFoundException e) {
Systemoutprintln("未找到指定路径的文件!");
eprintStackTrace();
}
}
}
Cell cell1 = sheetgetCell(i, j); //i为列 j为行
String result = cell1getContents();
我一直用jExcelApi,应该都差不多
你可以用result进行判断,把i和j保存下来不就知道了
以上就是关于如何用Apache POI *** 作Excel文件全部的内容,包括:如何用Apache POI *** 作Excel文件、java poi怎么获取excel单元格的内容、利用POI将数据库数据导入到Excel模版文件里,取某一行的问题,看下图等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)