我是一个Java初学者,如果您能为下面的情况提供一些示例代码或指南,我将不胜感激。
我有大量的html文件,每个文件包含一些学校的信息。每个html文件可能位于不同的文件夹路径层次结构中,但确保它始终位于文件夹路径的最低级别。有些文件夹可能没有学校的html文件。
例如
1文件夹中的C:\schools\england\london\hampstead\school_A.html 1 html 一个文件夹中的C:\schools\england\london\southwark\school_B.html多个文件 C:\schools\england\london\southwark\school_C.html C:\schools\england\london\southwark\school_D.html 不同路径级别的C:\schools\wales\monmouth\school_E.html文件 C:\学校\苏格兰\阿伯丁\阿伯丁文件夹没有文件
3栏标题:“学校”“寄宿类型”“每学期寄宿费”
Row 1: "**school_A**" "**Day,full boarding and weekly boarding**" "**£7,317 to £8,370**"非常感谢你的帮助
发布于 2015-06-01 14:27:24
我有一些关于这个要求的代码。请按照你的要求来做。
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文件。它将在文件名中创建具有今天日期的新文件。试试这个。我希望它能帮到你
https://stackoverflow.com/questions/30575994
复制相似问题