Java read excel xls and xlsx format support

1. tools
public class InExcelTool {

// read a specified position of a single
public static getContent String (String File, Page int, I int, int J) {
String S = null;
the try {
// parse
org.apache.poi Workbook = null .ss.usermodel.Workbook;
the NumberFormat of NF = NumberFormat.getInstance ();
// file suffix
String extString = file.substring (file.lastIndexOf () ".");
InputStream IS = new new FileInputStream (file);
IF ( "* .xls" .equals (extString)) {
Workbook = new new HSSFWorkbook (IS);
Sheet sheet = workbook.getSheetAt (page); // selected page sheet
HSSFRow row = (HSSFRow) sheet.getRow (
HSSFCell data = row.getCell(j);
s = String.valueOf(data);
if (s.lastIndexOf(".0") != -1) {
s = s.substring(0, s.indexOf("."));
}

}else if(".xlsx".equals(extString)){
workbook = new XSSFWorkbook(is);
Sheet sheet = (Sheet) workbook.getSheetAt(page); //选择sheet页
XSSFRow row = (XSSFRow) sheet.getRow(i);
XSSFCell data = row.getCell(j);
s = String.valueOf(data);
}else{
return "文件格式不符!";
}

}catch (Exception e){
e.printStackTrace ();
}
Return S;
}
}

2. calls

@RequestMapping ( "UpdateSocialCircleExcel") 
String UpdateSocialCircleExcel (@RequestParam ( "File") a MultipartFile File) {
the try {

// get the current system time
a Date = new new DATE a Date ();
// convert Chinese time
SimpleDateFormat dateFormat = new SimpleDateFormat ( " yyyy "+" of "+" mM "+" month "+" dd "+" Japan "+" hh "+" when "+" mm "+" points "+" ss "+" s - ");
/ name / get the file
String fileName = file.getOriginalFilename ();
// filename stitching
String FileSyetemTime = dateFormat.format (DATE) + fileName;
// specify the file storage path file.on behalf of separator / or \ to solve cross-platform / \ of cross-platform
// designated storage location
DestFileName = String "C:" + + File.separator "the Users" + File.separator + "Public" + File.separator + "Imports" + File.separator + FileSyetemTime;
// will be created in the local file
File pinjie = new File (destFileName ); // file path to the file name in the results stitching
String the destFile pinjie + = "";
file.transferTo (pinjie); // create added
       System.out.println(InExcelTool.getContent(destFile, 0, 2, 1));
    } 
}
Note: The above content is for personal use learning record, any questions, please caution!

Guess you like

Origin www.cnblogs.com/itxiaoxia/p/12090194.html