通过Java将HTML内容复制到Excel中

我是一名Java初学者,如果你能帮忙提供一些示例代码或指导下面的情况,将不胜感激

我有大量的html文件,每个文件都包含一些学校的信息。 每个html文件可能位于文件夹path的不同层次结构中,但肯定它始终位于文件夹path的最低级别。 而一些文件夹可能没有学校的HTML文件

例如

C:\ schools \ england \ london \ hampstead \ school_A.html [1个HTML文件夹] C:\ schools \ england \ london \ southwark \ school_B.html [多个文件夹在1个文件夹中] C:\ schools \ england \ london \ southwark \ school_C.html C:\ schools \ england \ london \ southwark \ school_D.html

C:\ schools \ wales \ monmouth \ school_E.html [不同path级别的文件] C:\ schools \ scotland \ aberdeen \ aberdeen [文件夹没有文件]

  • HTML内容将被复制

<h1 id =“MainControl_CustomFunctionality_ZoneMain_EmbeddedUserControlPlaceholderControl1_ctl01_schoolName”class =“schoolName”> school_A

日/登机types: 日,全日登机和每周登机

每学期的寄宿费用: 7,317英镑到8370英镑(约合人民币7,317元)

  • 预计的结果在EXCEL表中

3列标题:“学校”“登机牌”“每次登机费”

第1排:“ school_A ”“ 日,全面登机和每周登机 ”“ £7,317英镑至8,370英镑

非常感谢您的帮助

我有这个要求的一些代码。 请根据您的要求遵循此。

import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFFont; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class HTMLToExcel { public static void main(String[] args) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(new File("D:\\Excels\\log_km_styles1.html"))); // Create Work book XSSFWorkbook xwork = new XSSFWorkbook(); // Create Spread Sheet XSSFSheet xsheet = xwork.createSheet("MyFristSheet"); //Create Row (Row is inside spread sheet) XSSFRow xrow = null; int rowid =0; String line ; while (( line =br.readLine())!= null) { // Create font for applying bold or italic or same thing else on the content /*XSSFFont xfont = xwork.createFont(); xfont.setBoldweight(xfont.BOLDWEIGHT_BOLD); XSSFCellStyle xstyle = xwork.createCellStyle(); xstyle.setFont(xfont);*/ System.out.println(line); String split[] = line.split("<br>"); Cell cell; for (int i = 0; i < split.length; i++) { xrow = xsheet.createRow(rowid); cell = xrow.createCell(2); cell.setCellValue(split[i]); String[] columnSplit = split[i].split("\\W+"); int columnCount = 3; for (int j = 0; j < columnSplit.length; j++) { cell = xrow.createCell(columnCount++); cell.setCellValue(columnSplit[j]); } System.out.println(split[i]); rowid++; } } // create date for adding this to our workbook name like workbookname_date Date d1 = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy"); String todaysDate = sdf.format(d1); System.out.println(sdf.format(d1)); //Create file system using specific name FileOutputStream fout = new FileOutputStream(new File("D:\\Excels\\redaingfromHTMLFile_"+todaysDate+".xlsx")); xwork.write(fout); fout.close(); System.out.println("redaingfromHTMLFile_"+todaysDate+".xlsx written successfully" ); } catch (Exception e) { e.printStackTrace(); } } } 

以上代码将html文件内容转换为Excel文件。 它将在文件名称中创build具有今天date的新文件。 试试这个。 我希望这会帮助你