1、转换成JsonArray
public static JSONArray excleInGeneric(Workbook book, int sheetIndex, String[] propertys, boolean flag){
JSONArray ar = new JSONArray();
try {
Sheet sheet = book.getSheetAt(sheetIndex);
for(int i = 2;i <= getAccuracyContextNum(sheet);i++){
Row row = sheet.getRow(i);
JSONObject jsobj= new JSONObject();
for(int j = 1;j<propertys.length + 1;j++){
String fieldName = propertys[j-1];
String value = changeToString(row.getCell(j), flag).trim();
jsobj.put(fieldName, value);
}
ar.add(jsobj);
}
} catch (Exception e) {
logger.warn("" + e);
}
return ar;
}
public static String changeToString(Cell cell, boolean flag) throws Exception {
String returnv = "";
if (cell == null)
return returnv;
int type = cell.getCellType();
DecimalFormat df = new DecimalFormat("#.00");
switch (type) {
case Cell.CELL_TYPE_NUMERIC: {
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
if (DateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
DateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
returnv = formater.format(date);
break;
}
if (flag){
returnv = df.format(cell.getNumericCellValue());
} else {
cell.setCellType(Cell.CELL_TYPE_STRING);
returnv = cell.getRichStringCellValue().getString();
}
break;
}
case Cell.CELL_TYPE_STRING:
returnv = cell.getRichStringCellValue().getString();
break;
default:
break;
}
return returnv;
}
public static int getAccuracyContextNum(Sheet sheet) {
for (int i = 0; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
if (isRowEmpty(row)) {
int lastRowNum = sheet.getLastRowNum();
if (i >= 0 && i < lastRowNum) {
sheet.shiftRows(i + 1, lastRowNum, -1);
}
if (i == lastRowNum) {
if (row != null) {
sheet.removeRow(row);
}
}
i--;
}
}
return sheet.getLastRowNum();
}
public static boolean isRowEmpty(Row row){
for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
Cell cell = row.getCell(i);
if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK){
return false;
}else {
break;
}
}
return true;
}
2、xlsx导出
public static void outPut(HttpServletResponse response, List list,
String sheetName) throws Exception {
OutputStream os = response.getOutputStream();
response.reset();
response.setHeader("Content-disposition", "attachment; filename="
+ new String(sheetName.getBytes("GB2312"), "8859_1") + ".xlsx");
response.setContentType("application/vnd.ms-excel;charset=gb2312");
try {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
XSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 12);
font.setFontName(" 黑体 ");
cellStyle.setFont(font);
XSSFSheet sheet = wb.createSheet(sheetName);
XSSFRow rowHead = sheet.createRow(0);
rowHead.setHeightInPoints((short) 25);
Object[] headData = (Object[]) list.get(0);
for (int i = 0, n = headData.length; i < n; i++) {
if (headData[i] != null)
rowHead.createCell(i).setCellValue(headData[i].toString());
else {
rowHead.createCell(i).setCellValue("");
}
}
setCellStyle(cellStyle, sheet, rowHead);
for (int i = 1, num = list.size(); i < num; i++) {
try {
Object[] contentData = (Object[]) list.get(i);
XSSFRow excelRow = sheet.createRow(i);
excelRow.setHeightInPoints((short) 20);
for (int j = 0; j < contentData.length; j++) {
if (contentData[j] != null) {
excelRow.createCell(j).setCellValue(
contentData[j].toString());
} else {
excelRow.createCell(j).setCellValue("");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
if (os != null)
wb.write(os);
}catch (Exception e) {
e.printStackTrace();
} finally {
try {
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void setCellStyle(XSSFCellStyle cellStyle, XSSFSheet sheet,
XSSFRow row) {
for (short i = 0; i < row.getLastCellNum(); i++) {
Cell cell = row.getCell(i);
cell.setCellStyle(cellStyle);
}
}