This article describes the method of parsing Excel content in Java. Share it for your reference. The specific implementation method is as follows:
The code copy 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 Fire Level 3 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
Sheet sheet = null;// sheet page, since excel always has multiple sheet pages, it is necessary to determine which one is the specific value.
Row row1 = null;// A row in the Sheet page count
int colNum = 0;// Total Sheet row count
Cell cell = null;// Contents of the first column
Cell cell1 = null;// The content of the second column
String meetingname = null;// Point name
String meetid = null;// Point number
String meeting = null;// Key points
String meeting time = null;// Disposal time
MeetBean meet = null;
// Determine what format the file is 2003/2007 The object is handled differently depending on the version
if (filename.endsWith(".xls")) {
workbook = new HSSFWorkbook(is);// Excel 2003
} else if (filename.endsWith(".xlsx")) {
workbook = new XSSFWorkbook(is);// Excel 2007
} else {
return null;
}
// To determine which Sheet page to process, there are three users in total, and they will deal with different key points of handling each.
if (level == "401") {
// Key points and handling tips for the on-duty webmaster
sheet = workbook.getSheetAt(0);
colNum = sheet.getLastRowNum();// The total number of rows does not include the title content
System.out.println("Total:" + colNum + "row");
for (int i = 3; i <= colNum; i++) {
meet = new MeetBean();
row1 = sheet.getRow(i);// Number of rows to be parsed
cell = row1.getCell((short) 2);// The number of columns to resolve key points names
cell1 = row1.getCell((short) 4);// The number of columns to parse the key points prompts
if (cell != null && cell1 != null) {
meetname = cell.getStringCellValue();
meeting = cell1.getStringCellValue();
meetid = "YD" + i;
// If the name of the disposal point is empty, it will loop to the last disposal point and return. . .
if (!meetname.equals("")) {
String intstr = String
.valueOf((int) (Math.random() * 10 + 1));// Generate random numbers from 1-10
// If it is a random number of 1-9, the automatic zero-compensation time format is 00:00:00
if (intstr.length() < 2) {
String min = "0" + intstr;
meettime = "00:" + min + ":00";
} else {
meettime = "00:" + intstr + ":00";
}
meet.setMeetid(meetid);// Number of key points for disposal
meet.setMeetname(meetname);// The name of the key points of the disposal
meet.setMeethint(meethine);// 0 key points to deal with
meet.setMeettime(meettime);// Disposal time
meet.setMeetLevel("401");// Level of handling points
list.add(meet);
} else {
return list;
}
} else {
return list;
}
}
} else if (level == "402") {
sheet = workbook.getSheetAt(1);// OCC dispatcher's key points and handling tips
colNum = sheet.getLastRowNum();// The total number of rows does not include the title content
System.out.println("Total:" + colNum + "row");
for (int i = 3; i <= colNum; i++) {
meet = new MeetBean();
row1 = sheet.getRow(i);// Number of rows to be parsed
cell = row1.getCell((short) 2);// The number of columns to resolve key points names
cell1 = row1.getCell((short) 4);// The number of columns to parse the key points prompts
if (cell != null && cell1 != null) {
meetname = cell.getStringCellValue();
meeting = cell1.getStringCellValue();
meetid = "YD" + i;
// If the name of the disposal point is empty, it will loop to the last disposal point and return. . .
if (!meetname.equals("")) {
String intstr = String
.valueOf((int) (Math.random() * 10 + 1));// Generate random numbers from 1-10
// If it is a random number of 1-9, the automatic zero-compensation time format is 00:00:00
if (intstr.length() < 2) {
String min = "0" + intstr;
meettime = "00:" + min + ":00";
} else {
meettime = "00:" + intstr + ":00";
}
meet.setMeetid(meetid);// Number of key points for disposal
meet.setMeetname(meetname);// The name of the key points of the disposal
meet.setMeethint(meethine);// Key points for handling
meet.setMeettime(meettime);// Disposal time
meet.setMeetLevel("402");// Level of handling points
list.add(meet);
} else {
return list;
}
} else {
return list;
}
}
} else if (level == "403") {
sheet = workbook.getSheetAt(2);// Key points of the control center and handling tips
colNum = sheet.getLastRowNum();// The total number of rows does not include the title content
System.out.println("Total:" + colNum + "row");
int nameInt = 0;
int hineInt = 0;
for (int j = 0; j <= colNum; j++) {
row1 = sheet.getRow(3);// Only the number of rows to be parsed has the key points name in the third row, the prompt content
cell = row1.getCell((short) 1);// The number of columns to resolve key points names
cell1 = row1.getCell((short) 2);// The number of columns to parse the key points prompts
}
for (int i = 3; i <= colNum; i++) {
meet = new MeetBean();
row1 = sheet.getRow(i);// Number of rows to be parsed
cell = row1.getCell((short) 1);// The number of columns to resolve key points names
cell1 = row1.getCell((short) 2);// The number of columns to parse the key points prompts
if (cell != null && cell1 != null) {
meetname = cell.getStringCellValue();
meeting = cell1.getStringCellValue();
meetid = "YD" + i;
// If the name of the disposal point is empty, it will loop to the last disposal point and return. . .
if (!meetname.equals("")) {
String intstr = String
.valueOf((int) (Math.random() * 10 + 1));// Generate random numbers from 1-10
// If it is a random number of 1-9, the automatic zero-compensation time format is 00:00:00
if (intstr.length() < 2) {
String min = "0" + intstr;
meettime = "00:" + min + ":00";
} else {
meettime = "00:" + intstr + ":00";
}
meet.setMeetid(meetid);// Number of key points for disposal
meet.setMeetname(meetname);// The name of the key points of the disposal
meet.setMeethint(meethine);// Key points for handling
meet.setMeettime(meettime);// Disposal time
meet.setMeetLevel("403");// Level of handling points
list.add(meet);
} else {
return list;
}
} else {
return list;
}
}
}
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}
I hope this article will be helpful to everyone's Java programming.