How to fill in Excel file using java

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

    First: It's better to start with a working example and work your way from there. So start with the sample code that writes a simple string to a single cell a new sheet, then write to an existing sheet on a local filesystem, and only then write data you've parsed from the web. This way, when you run into problems, you've a better idea where to look for a solution.

    The exception you're listing is the generic exception that gets thrown by ZipPackage when saving fails:

    if (!defaultPartMarshaller.marshall(part, zos))
        throw new OpenXML4JException("The part " + part.getPartName().getURI()
        + " fail to be saved in the stream with marshaller " + defaultPartMarshaller);
    

    So the marshall method on the defaultPartMarshaller returns false and the internal exception which is the cause of the failure is lost. The DefaultMarshaller does not do much, it simply asks the part to save itself to the OutputStream.

    From there it gets a little less certain what kind of PackagePart is being saved. But for instance the ZipPartMarshaller catches any exceptions that occur and logs them before returning false:

    try {
        ...
    } catch (IOException ioe) {
        logger.log(POILogger.ERROR,"Cannot write: " + part.getPartName() + ": in ZIP",
            ioe);
        return false;
    }
    

    So could you take a look at the rest of the output, see if any more relevant info gets logged before this exception?

    If you cannot find more relevant logging, this is quite normal cause by default, the logger is a NullLogger which doesn't log a thing. Could you set the runtime property org.apache.poi.util.POILogger=org.apache.poi.util.SystemOutLogger (for example by starting java with command line argument -Dorg.apache.poi.util.POILogger=org.apache.poi.util.SystemOutLogger) and see if this produces more logging?

提交回复
热议问题