[英]reading data from web page and writing in excel
我正在嘗試從網頁中讀取數據並寫入excel。該網頁包含兩個表,這些表值被提取並將其寫入excel。當提取並打印它時一切正常,但是應該將其寫入在Excel的最后一行數據只寫在所有單元格?
我以為我已經在全球范圍內聲明了所需的數據,因此它變得無處不在。但是由於問題仍然存在,所以沒有用。
請在下面的代碼中查找並讓我現在出現任何錯誤。
Document doc=Jsoup.parse(singleFile, "UTF-8", "http://example.com/");
Element table=doc.select("table").get(0);
Element tabl=doc.select("table").get(1);
Elements rows1=tabl.select("tr");
Node Avg = null;
for(int i=1;i< rows1.size();i++){
Element row1=rows1.get(i);
Elements col=row1.select("td");
String Name =col.get(0).text();
Node Max=col.get(2).childNode(0);
Avg=col.get(3).childNode(0);
System.out.println("Name:"+Name+ ": Avg:"+Avg);
}
FileInputStream fis = new FileInputStream(new File("C:/Users/mramasa7/Desktop/metrics/GUX_BSL Metrics Data_JUN_2019.xlsx"));
Workbook workbook = new XSSFWorkbook(fis);
Sheet sheet = workbook.getSheetAt(0);
Cell cell=null;
cell=sheet.getRow(34).getCell(k);
cell.setCellValue(Avg.toString());
據我了解,您的問題是原始表上的最后一個值正在excel電子表格的每個單元格中打印。
在電子表格上編寫的代碼如下:
cell.setCellValue(Avg.toString());
您尚未將那段代碼添加到為數據源上的每個條目創建單元的循環中。 創建行時,應填充行。 使用Apache POI示例:
public byte[] export(List<String> exportList) throws Exception{
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Export");
for(int i=0; i< exportList.size(); i++) {
Row row = sheet.createRow(i);
row.createCell(0)
.setCellValue(exportList.get(i));
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
workbook.write(bos);
bos.close();
return bos.toByteArray();
}
要編輯現有的excel擴展名,您應使用循環遍歷所有現有行並以所需值填充的循環。 例:
for(int i=0; i< sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
row.getCell(0)
.setCellValue(exportList.get(i)); // Insert the number of the cell herer (column)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.