How to fill in Excel file using java

前端 未结 8 2096
离开以前
离开以前 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:38

    package knvbj;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.List;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.nodes.TextNode;
    import org.jsoup.select.Elements;
    
    public class KNVBJ {
    
    private static int Clnummer=1;
        public static void main(String[] args) throws IOException {       
            List urlList = ReadXlsx.readXlsx();
            urlList.get(1);
            for (String url : urlList) {
            System.out.println("url: " + url);
        }
    
        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); Elements anchors = finalDocument.getElementsByTag("a"); String email = anchors.get(1).text(); String fname = "/Users/muratcanpinar/Downloads/KNVBJ/src/knvbj/Voetbalclubs.xlsx"; InputStream inp = new FileInputStream(fname); Workbook wb = new XSSFWorkbook(inp); 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); } FileOutputStream out = new FileOutputStream("/Users/muratcanpinar/Downloads/KNVBJ/build/classes/knvbj/ClubI nformation.xlsx",true); wb.write(out); out.close(); } }

    you need to create output Stream after you created all cells,then write them to the file. see the codes in details.

提交回复
热议问题