How to fill in Excel file using java

前端 未结 8 2085
离开以前
离开以前 2021-01-05 05:08

I have the following code to fill in the Excel file, with information that I get from the Internet using Jsoup.

package knvbj;

import java.io.FileInputStrea         


        
8条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-05 05:35

    In the current code, you are trying to write the ClubInformation.xlsx into one sheet of Voetbalclubs.xlsx . Thus it is giving an error. (xlsx is a xml format, thus you get error while writing /docProps/app.xml).

    I have modified your code as below. Change the line List urlList = Arrays.asList("http://google.com"); as per your need. Let me know if this works

    public class KNVBJ {
    
    private static int Clnummer=1;
        public static void main(String[] args) throws IOException {
           FileOutputStream out = new FileOutputStream("ClubInformation.xlsx");
            List urlList = Arrays.asList("http://google.com");
            urlList.get(0);
            for (String url : urlList) {
                System.out.println("url: " + url);
            }
            String fname = "Voetbalclubs.xlsx";
            FileOutputStream output = new FileOutputStream(fname); 
            for (int i = 0; i < urlList.size(); i++) {
                Document doc = Jsoup.connect(urlList.get(i))
                        .data("query", "Java")
                        .userAgent("Mozilla")
                        .cookie("auth", "token")
                        .timeout(3000)
                        .post();
    
                Element content1 = doc.getElementsByClass("details").first();
                String body = content1.toString();
                Document docb = Jsoup.parseBodyFragment(body);
                Element bbd = docb.body();
                String kkj = bbd.toString();                
    
                Document finalDocument = Jsoup.parse(kkj);
                Element ClubName = finalDocument.getElementsByClass("title").first();
                String NameOfClub = ClubName.text();
                System.out.println(NameOfClub);    
    
                Element Adres = finalDocument.getElementsByClass("text").get(1);
    
                String[] addressParts = Adres.html().split("
    "); String SplitString; String PlaatsName; String Straat; String telNo; String Accommodatie; String Postcode; Accommodatie = addressParts[0].trim(); Straat = addressParts[1].trim(); SplitString = addressParts[2].trim(); telNo = addressParts[3].trim(); String splitted[]= SplitString.split(" "); Postcode = splitted[0]; PlaatsName = splitted[1]; System.out.println(Accommodatie + " " + Straat + " " + " postcode " + Postcode + " Plaatsname " + PlaatsName+ " "+ telNo); org.jsoup.select.Elements anchors = finalDocument.getElementsByTag("a"); String email = anchors.get(1).text(); Workbook wb = new XSSFWorkbook(); Sheet sheet = wb.getSheetAt(0); Row r1 = sheet.getRow(0); r1.createCell(Clnummer++).setCellValue(NameOfClub); r1.createCell(Clnummer++).setCellValue(Accommodatie); r1.createCell(Clnummer++).setCellValue(Straat); r1.createCell(Clnummer++).setCellValue(Postcode); r1.createCell(Clnummer++).setCellValue(PlaatsName); r1.createCell(Clnummer++).setCellValue(telNo); r1.createCell(Clnummer++).setCellValue(email); wb.write(output); } out.close(); } }

提交回复
热议问题