一、Excel解析
Excel主要是讀和寫,主要使用FileInputStream輸入流對象和FileOutStream輸出流對象
1、讀具體的Excel單元格
讀取單元格主要有打開Excel、獲取所有sheet、獲取指定sheet、獲取指定的row、獲取指定的cell、讀取單元格等步驟。
public static void main(String[] args) {
//讀取Excel
/*
1、打開Excel
2、獲取所有sheet
3、獲取指定sheet
4、獲取指定的row
5、獲取指定的cell單元格 6、讀取單元格內容 */
FileInputStream fis = null;
try {
//1、打開Excel
fis = new FileInputStream("src/test/resources/demo.xlsx");
//2、獲取所有sheet
Workbook sheets = WorkbookFactory.create(fis);
//3、獲取指定sheet
//Sheet sheet = sheets.getSheet("分數");
Sheet sheet = sheets.getSheetAt(0);
//4、獲取指定的row
Row row = sheet.getRow(2);
//5、獲取指定的cell單元格
Cell cell = row.getCell(1, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
//6、讀取單元格內容
String cellValue = cell.getStringCellValue();
System.out.println(cellValue);
fis.close();
} catch (Exception e) {
e.printStackTrace();
}finally {
//關流
try {
if (fis != null){
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
2、讀取所有內容
讀取Excel內容主要有普通for循環和增強for循環兩種方式,具體步驟有打開Excel、獲取所有sheet、獲取所有sheet、獲取所有row、獲取所有cell、獲取所有單元格內容。
public static void main(String[] args) {
//讀取Excel
/*
1、打開Excel
2、獲取所有sheet
3、獲取指定sheet
4、獲取所有的row
5、獲取所有的cell單元格 6、讀取單元格內容 */
FileInputStream fis = null;
try {
//1、打開Excel
fis = new FileInputStream("src/test/resources/demo.xlsx");
//2、獲取所有sheet
Workbook sheets = WorkbookFactory.create(fis);
//3、獲取指定sheet
Sheet sheet = sheets.getSheetAt(0);
//4、獲取所有的row
//4.1、普通for循環
int lastRowNum = sheet.getLastRowNum();
for (int i = 0; i<=lastRowNum;i++){ //循環行
Row row = sheet.getRow(i);
short lastCellNum = row.getLastCellNum();
for (int j=0; j<lastCellNum; j++){ //循環列
Cell cell = row.getCell(j, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
cell.setCellType(CellType.STRING);
String cellValue = cell.getStringCellValue();
System.out.print(cellValue + ",");
}
System.out.println();
}
//4.2、增強for循環
for (Row row : sheet){ //循環行
for (Cell cell :row){ //循環列
//強行轉換單元格類型String
cell.setCellType(CellType.STRING);
String cellValue = cell.getStringCellValue();
System.out.print(cellValue +",");
}
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//關流
try {
if(fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
3、讀入Excel表格內容並修改內容
使用FileInputStream讀取Excel內容,使用FileOutStream修改Excel內容。
public static void main(String[] args) throws Exception {
//excel寫
//寫:修改操作:讀取出,再寫入。
//1、打開excel
FileInputStream fis = new FileInputStream("src/test/resources/demo.xlsx");
//2、獲取所有sheets
Workbook sheets = WorkbookFactory.create(fis);
//3、獲取指定sheet
Sheet sheet = sheets.getSheetAt(0);
//4、獲取指定row
Row row = sheet.getRow(1);
//5、獲取指定cell單元格
Cell cell = row.getCell(2, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
//6、修改值
cell.setCellValue(100);
//7、關鍵輸出流對象
FileOutputStream fos = new FileOutputStream("src/test/resources/demo.xlsx");
//8、把java內存中的內容回寫到Excel文件中
sheets.write(fos);
//9、關流
fis.close();
fos.close();
4、同時修改Excel表格中的內容
public static void main(String[] args) throws Exception {
WriteBackData w1= new WriteBackData(1,2,"Pass");
WriteBackData w2= new WriteBackData(2,2,"Fail");
WriteBackData w3= new WriteBackData(3,2,"Pass");
List<WriteBackData> list = new ArrayList<>();
list.add(w1);
list.add(w2);
list.add(w3);
for (int i = 0; i<list.size(); i++){
WriteBackData backData = list.get(i);
int rowNum = backData.getRowNum();
int cellNum = backData.getCellNum();
String content = backData.getContent();
getCell(rowNum,cellNum,content);
}
}
public static String getCell(int rowNum,int cellNum,String content) throws Exception {
//1、打開excel
FileInputStream fis = new FileInputStream("src/test/resources/exam.xlsx");
//2、獲取所有sheet
Workbook sheets = WorkbookFactory.create(fis);
//3、獲取指定sheet
Sheet sheet = sheets.getSheetAt(0);
//4、獲取指定row
Row row = sheet.getRow(rowNum);
Cell cell = row.getCell(cellNum, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
cell.setCellValue(content);
FileOutputStream fos = new FileOutputStream("src/test/resources/exam.xlsx");
sheets.write(fos);
fis.close();
fos.close();
return content;
}
二、xml
xml:Extensible Markup Language,擴展性標記語言 HTML,主要特點是可擴展性,在遵循xml語法的前提下支持自定義和修改。
xml主要使用dom4j解析技術,dom4j主要步驟為:添加依賴、創建解析器SaxReader對象、獲取document對象、獲取根元素、獲取根元素下的子元素。
public static void main(String[] args) throws Exception {
//xml讀 (了解) dom4j
//1、xml讀取對象
SAXReader reader = new SAXReader();
FileInputStream fis = new FileInputStream("src/test/resources/student.xml");
//2、整顆dom樹
Document document = reader.read(fis);
//3、獲取root標籤
Element rootElement = document.getRootElement();
System.out.println(rootElement.getName());
//4、獲取root標籤下的一級子標籤
List<Element> subElements1 = rootElement.elements();
for (Element element :subElements1){
System.out.println(element.getName()+"===="+element.getData());
//4.1、獲取屬性
Attribute id = element.attribute("id");
if (id != null){
System.out.println(id.getName()+"==="+id.getData());
}
//5、獲取root標籤下的二級子標籤
List<Element> subElements2 = element.elements();
if (subElements2 != null && subElements2.size()>0){
for (Element element1 :subElements2){
System.out.println(element1.getName()+"===="+element1.getData());
}
}
}
fis.close();
}