Java method for parsing Excel content
- 2020-04-01 03:44:44
- OfStack
This article illustrates how Java parses Excel content. Share with you for your reference. The specific implementation method is as follows:
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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.xssf.usermodel.XSSFWorkbook;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
meetquery("403", "e:\Excel\1 Three level fire plan disposal process .xlsx");
}
private static ArrayList<MeetBean> meetquery(String level, String filename) {
ArrayList<MeetBean> list = new ArrayList<MeetBean>();
try {
File file = new File(filename);
InputStream is = new FileInputStream(file);
Workbook workbook = null;//A workbook object is an Excel file & NBSP; < br / >
Sheet sheet = null;//Sheet page, because excel always has multiple sheet pages, need to determine the specific value of which & NBSP; < br / >
Row row1 = null;//A line in a Sheet & NBSP; < br / >
int colNum = 0;//Total number of lines of Sheet & NBSP; < br / >
Cell cell = null;//The first column & NBSP; < br / >
Cell cell1 = null;//The second column & cake; < br / >
String meetname = null;//Key points & NBSP; < br / >
String meetid = null;//Key points number & NBSP; < br / >
String meethine = null;//Key tips & NBSP; < br / >
String meettime = null;//Disposal time & NBSP; < br / >
MeetBean meet = null;
//Determining the format of the document 2003/2007 depends on the version of the disposal object & NBSP; < br / >
if (filename.endsWith(".xls")) {
workbook = new HSSFWorkbook(is);// Excel 2003
} else if (filename.endsWith(".xlsx")) {
workbook = new XSSFWorkbook(is);// Excel 2007
} else {
return null;
}
//
if (level == "401") {
//Treatment of the master on duty main points, treatment tips & NBSP; < br / >
sheet = workbook.getSheetAt(0);
colNum = sheet.getLastRowNum();//The total number of lines does not include the title content & NBSP; < br / >
System.out.println(" A total of: " + colNum + " line ");
for (int i = 3; i <= colNum; i++) {
meet = new MeetBean();
row1 = sheet.getRow(i);//Number of lines to parse & NBSP; < br / >
cell = row1.getCell((short) 2);//The number of columns to resolve the name of the main point & NBSP; < br / >
cell1 = row1.getCell((short) 4);//The number of columns you want to parse the main prompt & NBSP; < br / >
if (cell != null && cell1 != null) {
meetname = cell.getStringCellValue();
meethine = cell1.getStringCellValue();
meetid = "YD" + i;
//If the disposal point name is empty, the last disposal point is looped, and returns... < br / >
if (!meetname.equals("")) {
String intstr = String
.valueOf((int) (Math.random() * 10 + 1));//Generate 1-10 random Numbers & NBSP; < br / >
//If it is 1-9 random number, the time format of 00:00:00& NBSP; is required. < br / >
if (intstr.length() < 2) {
String min = "0" + intstr;
meettime = "00:" + min + ":00";
} else {
meettime = "00:" + intstr + ":00";
}
meet.setMeetid(meetid);//Disposal key number & NBSP; < br / >
meet.setMeetname(meetname);//Name of disposal points & cake; < br / >
meet.setMeethint(meethine);//Handling 0 tips & instructions; < br / >
meet.setMeettime(meettime);//Disposal time & NBSP; < br / >
meet.setMeetLevel("401");//Treatment key level & NBSP; < br / >
list.add(meet);
} else {
return list;
}
} else {
return list;
}
}
} else if (level == "402") {
sheet = workbook.getSheetAt(1);//OCC dispatcher's disposal notes & instructions; < br / >
colNum = sheet.getLastRowNum();//The total number of lines does not include the title content & NBSP; < br / >
System.out.println(" A total of: " + colNum + " line ");
for (int i = 3; i <= colNum; i++) {
meet = new MeetBean();
row1 = sheet.getRow(i);//Number of lines to parse & NBSP; < br / >
cell = row1.getCell((short) 2);//The number of columns to resolve the name of the main point & NBSP; < br / >
cell1 = row1.getCell((short) 4);//The number of columns you want to parse the main prompt & NBSP; < br / >
if (cell != null && cell1 != null) {
meetname = cell.getStringCellValue();
meethine = cell1.getStringCellValue();
meetid = "YD" + i;
//If the disposal point name is empty, the last disposal point is looped, and returns... < br / >
if (!meetname.equals("")) {
String intstr = String
.valueOf((int) (Math.random() * 10 + 1));//Generate 1-10 random Numbers & NBSP; < br / >
//If it is 1-9 random number, the time format of 00:00:00& NBSP; is required. < br / >
if (intstr.length() < 2) {
String min = "0" + intstr;
meettime = "00:" + min + ":00";
} else {
meettime = "00:" + intstr + ":00";
}
meet.setMeetid(meetid);//Disposal key number & NBSP; < br / >
meet.setMeetname(meetname);//Name of disposal points & cake; < br / >
meet.setMeethint(meethine);//Handling tips & NBSP; < br / >
meet.setMeettime(meettime);//Disposal time & NBSP; < br / >
meet.setMeetLevel("402");//Treatment key level & NBSP; < br / >
list.add(meet);
} else {
return list;
}
} else {
return list;
}
}
} else if (level == "403") {
sheet = workbook.getSheetAt(2);//Handling key points, handling tips & instructions of the control center; < br / >
colNum = sheet.getLastRowNum();//The total number of lines does not include the title content & NBSP; < br / >
System.out.println(" A total of: " + colNum + " line ");
int nameInt = 0;
int hineInt = 0;
for (int j = 0; j <= colNum; j++) {
row1 = sheet.getRow(3);//The number of lines to be resolved is only the third line with the handle name, prompt & PI; < br / >
cell = row1.getCell((short) 1);//The number of columns to resolve the name of the main point & NBSP; < br / >
cell1 = row1.getCell((short) 2);//The number of columns you want to parse the main prompt & NBSP; < br / >
}
for (int i = 3; i <= colNum; i++) {
meet = new MeetBean();
row1 = sheet.getRow(i);//Number of lines to parse & NBSP; < br / >
cell = row1.getCell((short) 1);//The number of columns to resolve the name of the main point & NBSP; < br / >
cell1 = row1.getCell((short) 2);//The number of columns you want to parse the main prompt & NBSP; < br / >
if (cell != null && cell1 != null) {
meetname = cell.getStringCellValue();
meethine = cell1.getStringCellValue();
meetid = "YD" + i;
//If the disposal point name is empty, the last disposal point is looped, and returns... < br / >
if (!meetname.equals("")) {
String intstr = String
.valueOf((int) (Math.random() * 10 + 1));//Generate 1-10 random Numbers & NBSP; < br / >
//If it is 1-9 random number, the time format of 00:00:00& NBSP; is required. < br / >
if (intstr.length() < 2) {
String min = "0" + intstr;
meettime = "00:" + min + ":00";
} else {
meettime = "00:" + intstr + ":00";
}
meet.setMeetid(meetid);//Disposal key number & NBSP; < br / >
meet.setMeetname(meetname);//Name of disposal points & cake; < br / >
meet.setMeethint(meethine);//Handling tips & NBSP; < br / >
meet.setMeettime(meettime);//Disposal time & NBSP; < br / >
meet.setMeetLevel("403");//Treatment key level & NBSP; < br / >
list.add(meet);
} else {
return list;
}
} else {
return list;
}
}
}
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}
I hope this article has been helpful to your Java programming.