add method for get all Excel sheet's names

This commit is contained in:
lxyer 2018-09-21 12:45:54 +08:00
parent 352c5934e3
commit 40a64e6392

View File

@ -134,7 +134,6 @@ public class ExcelKit {
* @throws IOException
* @date 2015-3-13 上午9:00:36
*/
@SuppressWarnings("all")
private static boolean downloadExcel(Workbook wb, String xlsName, HttpServletRequest request, HttpServletResponse response) throws IOException{
if(request.getHeader("user-agent").indexOf("MSIE") != -1) {
xlsName = java.net.URLEncoder.encode(xlsName,"utf-8") + ".xls";
@ -175,56 +174,31 @@ public class ExcelKit {
if(nRow == null){
nRow = sheet.createRow(r);
}
// nRow.setColumnWidth(r, arg1);
Cell nCell = nRow.createCell(c);
//根据不同类型进行转化如有其它类型没有考虑周全的使用发现的时候添加
char type = 'x';
if(o instanceof Integer){
type = 1;
}else if(o instanceof Double){
type = 2;
}else if(o instanceof Float){
type = 3;
}else if(o instanceof String){
type = 4;
}else if(o instanceof Date){
type = 5;
}else if(o instanceof Calendar){
type = 6;
}else if(o instanceof Boolean){
type = 7;
}else if(o == null) {
type = 8;
}
switch (type) {
case 1:nCell.setCellValue((Integer)o);break;
case 2:nCell.setCellValue((Double)o);break;
case 3:nCell.setCellValue((Float)o);break;
case 4:nCell.setCellValue((String)o);break;
case 5:nCell.setCellValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(o));break;
case 6:nCell.setCellValue((Calendar)o);break;
case 7:nCell.setCellValue((Boolean)o);break;
case 8:nCell.setCellValue("");break;
default:nCell.setCellValue(o + "");break;
}
if (o instanceof Integer) nCell.setCellValue((Integer)o);
else if (o instanceof Double) nCell.setCellValue((Double)o);
else if (o instanceof Float) nCell.setCellValue((Float)o);
else if (o instanceof String) nCell.setCellValue((String)o);
else if (o instanceof Date) nCell.setCellValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(o));
else if (o instanceof Calendar) nCell.setCellValue((Calendar)o);
else if (o instanceof Boolean) nCell.setCellValue((Boolean)o);
else if (o == null) nCell.setCellValue("");
else nCell.setCellValue(o + "");
}
//======================= 读取excel ===============================
/**
* read excel head
* @param file
* @param fields
* @param lastRowNum hope get row count
* @return
* @throws IOException
*/
public static List<Map> readExcelHead(File file, String[] fields, int lastRowNum) throws IOException {
return readExcel(file, fields, lastRowNum, null);
//read excel head
public static Map readExcelHead(File file, String[] fields) throws IOException {
List<Map> list = readExcel(file, fields, 1, null);
return list.size() > 0 ? list.get(0) : new HashMap();
}
//read excel head by sheetName
public static Map readExcelHead(File file, String[] fields, String sheetName) throws IOException {
List<Map> list = readExcel(file, fields, 1, sheetName);
return list.size() > 0 ? list.get(0) : new HashMap();
}
//read excel sheet[0]
@ -402,5 +376,21 @@ public class ExcelKit {
}
return true;
}
//get all sheet names
public static List<String> getSheetNames(File file) throws IOException {
Workbook wk;
try {
wk = new HSSFWorkbook(new FileInputStream(file));//if excel version 2007+ will throws OfficeXmlFileException
}catch (OfficeXmlFileException e){
wk = new XSSFWorkbook(new FileInputStream(file));
}
List<String> sheetNames = new ArrayList<>();
for (int i = 0; i < wk.getNumberOfSheets(); i++) {
sheetNames.add(wk.getSheetAt(i).getSheetName());
}
return sheetNames;
}
}