Quantcast
Channel: Java Programming Forum - Learn Java Programming - Apache POI
Viewing all 120 articles
Browse latest View live

Excel to XML Conversion

$
0
0
Hi,
I want to convert Excel to XML which should include text as well as images, shapes ect.
There is any way we can get openxml (Office Open XML) using POI API ?

I can able to get xml using Save As option with type "XML Spreadsheet 2003". But its only help to transform text where I m interested in images or at least its location.

Can any one guide?

Thanks.

import embedded files like xls in a new ppt slide

$
0
0
Hi,
My problem is that, i can't import a embedded xls-file in to a ppt slide.
Does anyone have a solution for this problem?
Thank you for your help.

Here is my code:

class createPPT {

public static void main(String[] args) throws IOException, OpenXML4JException, XmlException{

//load existing ppt-file with embedded files
XMLSlideShow ppt = new XMLSlideShow(new FileInputStream("C:\\Users\\..."));

//get the layout of the slide master
XSLFSlideLayout layout_ppt = ppt.getSlideMasters()[0].getLayout(ppt.getSlideMasters()[0].getSlideLayouts()[0].getType());

//create a new ppt-slide
XSLFSlide slide = ppt.createSlide(layout_ppt);

//save all embedded files in listemb, the first item is a xls-file
java.util.List<PackagePart> listemb = ppt.getAllEmbedds();

//load xls-file in poifs
POIFSFileSystem poifs = new POIFSFileSystem(listemb.get(0).getInputStream());

//here i want to import the embedded xls-file in a new ppt-slide
...

//save new ppt-file
FileOutputStream out = new FileOutputStream("C:\\Users\\...");
ppt.write(out);
out.close();
}
}

POI and Macros in Powerpoint

$
0
0
Hi,

I've tried to modify a powerpoint presentation which contains a macro (eg. "Test.pptm"). After saving the presentation and opening it with powerpoint, powerpoint crashes when I try to open the macros??
What am I doing wrong?

This is how I open the presentation:
Code:

// (templateStream being an InputStream to my report.pptm file).
this.pkg = OPCPackage.open(templateStream);
this.ppt = new XMLSlideShow(pkg);

This is how I store the presentation:
Code:

OutputStream os = new FileOutputStream(new File(fileName));
pkg.save(os);
os.close();

I tried POI version 3.9 and 3.10 FINAL with no success. I'm using Office 10.
Everything works fine when I remove the macro and use the extension ".pptx"

Kind regards,
Frank

"Colspan" cell

$
0
0
Hi,

i have this code:
Code:

XSSFRow row = this.sheet.getRow(1);
for (Cell cell : row) {
// cell get colspan(number columns used)
}

my cell is colspaned, it is used(4 columns). How i can get the colspan columns of my cell ?

thanks

Pdfbox

$
0
0
Hi ,
Can anyone tell me how to identify table in PDF using PDFBOX .pls

Add image to WORD in header doc or docx

$
0
0
Hi everyone
I have the following 2 classes which add an image to the body of a word document.
Now I need to add an image but to the header.
Please provide me with the solution.
Thanks all!
I have attached the image of the libraries I used apache poi 3.10
And the image of the error when I open the word document when I try to add the image
Class 1:
Code:



import java.io.*;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.*;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;

public class ImagesDoc {
    public static void main(String[] args) throws IOException,
                                                  InvalidFormatException {
        String raiz = "C:\\Users\\diflores\\Desktop\\";
        CustomXWPFDocument document = new CustomXWPFDocument(new FileInputStream(new File(raiz+"doc1.docx")));
        FileOutputStream fos = new FileOutputStream(new File(raiz+"doc2.doc"));
       
      XWPFHeaderFooterPolicy policy = document.getHeaderFooterPolicy();
      if (policy.getDefaultHeader() == null && policy.getFirstPageHeader() == null
            && policy.getDefaultFooter() == null) {
        // Need to create some new headers
        // The easy way, gives a single empty paragraph
        XWPFHeader headerD = policy.createHeader(policy.DEFAULT);
        //headerD.getParagraphs().get(0).createRun().setText("Hello Header World!");//this works I NEED to ADD AN IMAGE
        /*headerD.getParagraphs().get(0).createRun().addPicture(new FileInputStream(new File(raiz+"Captura.png")),
                                                              Document.PICTURE_TYPE_PNG, "Captura.png", 426, 389);*///ERROR HERE
        // Or the full control way
          CTP ctP1 = CTP.Factory.newInstance();
          CTR ctR1 = ctP1.addNewR();
          CTText t = ctR1.addNewT();
          t.setStringValue("Paragraph in header");

          XWPFParagraph p1 = new XWPFParagraph(ctP1, document);
          XWPFParagraph[] pars = new XWPFParagraph[1];
          pars[0] = p1;

          policy.createHeader(policy.FIRST, pars);
      } else {
        // Already has a header, change it
      }
     
        String id = document.addPictureData(new FileInputStream(new File(raiz+"Captura.png")),
                                            Document.PICTURE_TYPE_PNG);
        document.createPicture(id,document.getNextPicNameNumber(Document.PICTURE_TYPE_PNG),
                              426, 389);//W,H
        document.write(fos);
        fos.flush();
        fos.close();
    }
}


//second class

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlToken;

import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;

import java.io.IOException;
import java.io.InputStream;

public class CustomXWPFDocument extends XWPFDocument {
   
    public CustomXWPFDocument(InputStream in) throws IOException {
        super(in);
    }

    public void createPicture(String blipId, int id, int width, int height) {
        final int EMU = 9525;
        width *= EMU;
        height *= EMU;
        //String blipId = getAllPictures().get(id).getPackageRelationship().getId();

        CTInline inline = createParagraph().createRun().getCTR().addNewDrawing().addNewInline();
        String picXml =
            "" + "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" +
            "  <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
            "      <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
            "        <pic:nvPicPr>" + "            <pic:cNvPr id=\"" + id +
            "\" name=\"Generated\"/>" + "            <pic:cNvPicPr/>" +
            "        </pic:nvPicPr>" + "        <pic:blipFill>" +
            "            <a:blip r:embed=\"" + blipId +
            "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>" +
            "            <a:stretch>" + "              <a:fillRect/>" +
            "            </a:stretch>" + "        </pic:blipFill>" +
            "        <pic:spPr>" + "            <a:xfrm>" +
            "              <a:off x=\"0\" y=\"0\"/>" +
            "              <a:ext cx=\"" + width + "\" cy=\"" + height +
            "\"/>" + "            </a:xfrm>" +
            "            <a:prstGeom prst=\"rect\">" +
            "              <a:avLst/>" + "            </a:prstGeom>" +
            "        </pic:spPr>" + "      </pic:pic>" +
            "  </a:graphicData>" + "</a:graphic>";
        //CTGraphicalObjectData graphicData = inline.addNewGraphic().addNewGraphicData();
        XmlToken xmlToken = null;
        try {
            xmlToken = XmlToken.Factory.parse(picXml);
        } catch (XmlException xe) {
            xe.printStackTrace();
        }
        inline.set(xmlToken);
        //graphicData.set(xmlToken);

        inline.setDistT(0);
        inline.setDistB(0);
        inline.setDistL(0);
        inline.setDistR(0);

        CTPositiveSize2D extent = inline.addNewExtent();
        extent.setCx(width);
        extent.setCy(height);

        CTNonVisualDrawingProps docPr = inline.addNewDocPr();
        docPr.setId(id);
        docPr.setName("Picture " + id);
        docPr.setDescr("Generated");
    }
}

Attached Images

Apache POI excel sheet formula reference to another sheet.

$
0
0
I have a excel sheet which has a formula reference to another sheet of the same workbook.
Currently, I'm on sheet1 and trying to set a formula for a cell using cell.setFormula(Sheet2[#All]).
While I'm doing that, I'm encountering the following error-
Specified named range 'Sheet2' does not exist in the current workbook. org.apache.poi.ss.formula.FormulaParseException
at org.apache.poi.ss.formula.FormulaParser.parseNonRa nge(FormulaParser.java:569)
at org.apache.poi.ss.formula.FormulaParser.parseRange able(FormulaParser.java:429)
at org.apache.poi.ss.formula.FormulaParser.parseRange Expression(FormulaParser.java:268)
at org.apache.poi.ss.formula.FormulaParser.parseSimpl eFactor(FormulaParser.java:1119)
at org.apache.poi.ss.formula.FormulaParser.percentFac tor(FormulaParser.java:1079)
at org.apache.poi.ss.formula.FormulaParser.powerFacto r(FormulaParser.java:1066)
at org.apache.poi.ss.formula.FormulaParser.Term(Formu laParser.java:1426)
at org.apache.poi.ss.formula.FormulaParser.additiveEx pression(FormulaParser.java:1526)
at org.apache.poi.ss.formula.FormulaParser.concatExpr ession(FormulaParser.java:1510)
at org.apache.poi.ss.formula.FormulaParser.comparison Expression(FormulaParser.java:1467)
at org.apache.poi.ss.formula.FormulaParser.Arguments( FormulaParser.java:1051)
at org.apache.poi.ss.formula.FormulaParser.function(F ormulaParser.java:936)


However, in the workbook, I have created the necessary sheet. The sheet name is 'Sheet2'. But still the code is not able to refer to that sheet.
Please let me know if there is a way to fix this issue or any workaround.

Thanks.

addTab() missing

$
0
0
Hello everyone,

I'm using the Apache POI (poi-3.10.1) in order to generate a docx-document. The documentation says that there is a method, called addTab().
But when I'm trying to call this method during a XWPFRun, a get an error, because the method is missing (see my attachment).

Probably I make a mistake, do you have any idea?

Best regards and thanks in advance,

zitb
Attached Images

docx to pdf

$
0
0
Hi guys,
i am unable to convert docx into pdf and am using latest 1.0.4 libraries.I am unable to attach failure document.As i am completely blocked here can anyone suggest this.

Code Snippet:

InputStream is = new FileInputStream(new File(
"docx/Sample.docx"));
BufferedInputStream st=new BufferedInputStream(is);
XWPFDocument document = new XWPFDocument(st);

// 2) Prepare Pdf options
PdfOptions options = null;

// 3) Convert XWPFDocument to Pdf
OutputStream out = new FileOutputStream(new File(
"pdf/Sample.pdf"));
//document.write(out);
//out.close();
PdfConverter.getInstance().convert(document, out, options);
out.close();



Libraries used:
commons-codec-1.5.jar
dom4j-1.6.1.jar
fr.opensagres.xdocreport.itext.extension-1.0.4.jar
itext-2.1.7.jar
ooxml-schemas-1.1.jar
org.apache.poi.xwpf.converter.core-1.0.4.jar
org.apache.poi.xwpf.converter.pdf-1.0.4.jar
org.apache.poi.xwpf.converter.xhtml-1.0.4.jar
poi-3.9.jar
poi-ooxml-3.9.jar
stax-api-1.0.1.jar
xml-apis-1.0.b2.jar
xmlbeans-2.6.0.jar
xmlbeans-xmlpublic-2.6.0.jar

Error:
org.apache.poi.xwpf.converter.core.XWPFConverterEx ception: org.apache.xmlbeans.impl.values.XmlValueOutOfRange Exception: Invalid integer value: 720.0
at org.apache.poi.xwpf.converter.pdf.PdfConverter.doC onvert(PdfConverter.java:59)
at org.apache.poi.xwpf.converter.pdf.PdfConverter.doC onvert(PdfConverter.java:37)
at org.apache.poi.xwpf.converter.core.AbstractXWPFCon verter.convert(AbstractXWPFConverter.java:45)
at com.word.pdf.wordtopdf.createPDF(wordtopdf.java:37 )
at com.word.pdf.wordtopdf.main(wordtopdf.java:18)
Caused by: org.apache.xmlbeans.impl.values.XmlValueOutOfRange Exception: Invalid integer value: 720.0
at org.apache.xmlbeans.impl.values.XmlObjectBase$Valu eOutOfRangeValidationContext.invalid(XmlObjectBase .java:285)
at org.apache.xmlbeans.impl.values.JavaIntegerHolder. lex(JavaIntegerHolder.java:50)
at org.apache.xmlbeans.impl.values.JavaIntegerHolderE x.set_text(JavaIntegerHolderEx.java:40)
at org.apache.xmlbeans.impl.values.XmlObjectBase.upda te_from_wscanon_text(XmlObjectBase.java:1135)
at org.apache.xmlbeans.impl.values.XmlObjectBase.chec k_dated(XmlObjectBase.java:1274)
at org.apache.xmlbeans.impl.values.JavaIntegerHolder. bigIntegerValue(JavaIntegerHolder.java:58)
at org.apache.xmlbeans.impl.values.XmlObjectBase.getB igIntegerValue(XmlObjectBase.java:1504)
at org.openxmlformats.schemas.wordprocessingml.x2006. main.impl.CTPageMarImpl.getHeader(Unknown Source)
at org.apache.poi.xwpf.converter.pdf.internal.PdfMapp er.visitHeader(PdfMapper.java:180)
at org.apache.poi.xwpf.converter.pdf.internal.PdfMapp er.visitHeader(PdfMapper.java:112)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVis itor.visitHeaderRef(XWPFDocumentVisitor.java:1098)
at org.apache.poi.xwpf.converter.core.MasterPageManag er.visitHeadersFooters(MasterPageManager.java:213)
at org.apache.poi.xwpf.converter.core.MasterPageManag er.addSection(MasterPageManager.java:180)
at org.apache.poi.xwpf.converter.core.MasterPageManag er.compute(MasterPageManager.java:127)
at org.apache.poi.xwpf.converter.core.MasterPageManag er.initialize(MasterPageManager.java:90)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVis itor.visitBodyElements(XWPFDocumentVisitor.java:22 7)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVis itor.start(XWPFDocumentVisitor.java:194)
at org.apache.poi.xwpf.converter.pdf.PdfConverter.doC onvert(PdfConverter.java:55)
... 4 more


Regards,
Kumar

read values of userform Textbox and Checkbox

$
0
0
Hi,

I have an excel sheet that is used as a form (created using Developer mode). It contains labels, textboxes and checkboxes.
Using POI I have managed to get a list of these objects as instance of HSSFObjectData (using Scala):
Code:

  val file = new FileInputStream( new File("c:\\tmp\\test.xls"))
  val workbook = new HSSFWorkbook(file)
 
  val sheet = workbook.getSheetAt(0)
    val pat = sheet.getDrawingPatriarch
   
    pat.getChildren.asScala.toList.foreach { shape=>

      if(shape.isInstanceOf[HSSFObjectData]) {
        val box = shape.asInstanceOf[HSSFObjectData]
        if(box.getOLE2ClassName == "Forms.TextBox.1") {
              println(box.getWrapText.toString) //==> 0
          }
        }
      }

But from HSSFObjectData, I cannot see how to access the text value of a textbox or the selected value of a checkbox.
This information seems to be accessible from HSSFTextbox and HSSFCheckbox object, but I have not managed to find a way to access these instances.

Can anybody give me a hint ?

Best Regards,
Olivier

read and select dropdown list on xlsm file.

$
0
0
I have a xlsm file with multiples dropdown list.

This is my first time i'm using java and apache poi.
How can i read a dropdown list from cell and select the option?.

check checkbox in xlsm file with POI

$
0
0
I have multiples checkboxes in my xlsm file.

I would like to know how to check/uncheck a checkbox using POI.

I attached an image with an example of a cell.
Attached Images

Is it possible to extract property details from MSI installable using POI?

$
0
0
Could anyone advice whether it is possible to read the property table details from an MSI installable file using POI?
I need to extract the product name and product version details from the property table.

Thanks

Is there any way to uncheck textboxes, footnotes and endnotes

$
0
0
Hi,

Is there anyway to uncheck the textboxes, footnotes and endnotes option while creating word docx using POI. Suggest some idea

create Theme in new XSSFWorkbook

$
0
0
Is it is possible to create empty Theme if I create XSSFWorkbook by constructor XSSFWorkbook()?
I tried to do this, but there is a lot of fields (like Relations) to fill when I create Theme - and finally I have to do something like reading inputStream. But I don’t have IS, because I want create new workbook and fill everything using data from external JSON file.
Creating Theme is so complicated, so I tried to do this in some other ways too:
1) I tried to: import empty XLSX file to ApachePOI, get Theme from it and set this Theme to my new XSSFWorkbook. But then my new XLSX file doesn’t have file /xl/theme/theme1.xmland informations:
Code:

<Override ContentType="application/vnd.openxmlformats-officedocument.theme+xml" PartName="/xl/theme/theme1.xml" />
in content_types.xml,
Code:

<Override ContentType="application/vnd.openxmlformats-officedocument.theme+xml" PartName="/xl/theme/theme1.xml" />
in /xl/_rels/workbook.xml.rels.
2) Then I tried to not create new XSSFWorkbook, but: import external XLSX file, change its Theme and save again as XLSX. Then file has informations about Theme, but about original - not changed.
I checked that constructor XSSFWorkbook() creates objects like StylesSource and SharedStringsTable, but not ThemesTable. So maybe it’ impossible to change Theme after constructor, because in fact it changes nothing?
Any suggestions? Please, help, I’ve been trying for 3 days.

Set Image undertext with Apache-Poi

$
0
0
I have insert a image in a document .docx but i must set undertext.
Code to insert image:
Code:


                                    XWPFRun xwpfRun = p.createRun();
                                    String imgFile = valore.get(x)[0];
                                    int larg =100;
                                    int alt =100;
                                    int format = XWPFDocument.PICTURE_TYPE_PNG;
                                    try {
                                        xwpfRun.addPicture(new FileInputStream(imgFile), format, imgFile, Units.toEMU(larg), Units.toEMU(alt));
                                    } catch (Exception ex) {

                                    }

With this code i insert image without problem but this is with normal Layout and i dont know how to set undertext with code.

Sorry for my bad english and i hope somebody can help me :(sweat):

How can I create nestedTables XWPF

$
0
0
Hello, I have some trouble with POI.

It's not possible for me to create nestedTables. I tested some things but no one get the wished result.

It's possible for me to load a document with nested tables, manipulate it and save it.
But I can't create a table with another table inside with poi-methods.

Currently I used follow code.

Code:

       
       
        XWPFDocument document = new XWPFDocument();
        XWPFTable tableOne = document.createTable();
        XWPFTableRow tableOneRow1 = tableOne.getRow(0);
        tableOneRow1.addNewTableCell();
       
        CTTbl ctTable = CTTbl.Factory.newInstance();
        XWPFTable table = new XWPFTable(ctTable, document, 0, 0);
        XWPFTableRow tr = table.getRow(0);
        XWPFTableCell cell = tr.getCell(0);
        cell.setText("c");
       
      tableOne.getRow(0).getCell(0).setText("a");
        tableOne.getRow(0).getCell(1).setText("b");
        //tableOne.getRow(0).getCell(0).insertTable(0, document.createTable(1, 1));
        //tableOne.getRow(0).getCell(0).insertTable(0, table);
        tableOne.getRow(0).getCell(0).addParagraph();
        tableOne.getRow(0).getCell(0).getBodyElements().get(0).getBody().insertTable(0, table);
        tableOne.getRow(0).getCell(0).getParagraphs().get(0).createRun();
        tableOne.getRow(0).getCell(0).getParagraphs().get(0).getRuns().get(0).setText("d");
        tableOne.getRow(0).getCell(0).getTables().get(0).getRow(0).getCell(0).setText("c");
        return document;

The Document structure looks right.

documentStructure.PNG


But when I save the document then the first cell don't have the table in the xml-structure.

Code:

<?xml version="1.0" encoding="UTF-8"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:body>
    <w:tbl>
        <w:tblPr>
            <w:tblW w:w="0" w:type="auto"/>
            <w:tblBorders>
                <w:top w:val="single"/>
                <w:left w:val="single"/>
                <w:bottom w:val="single"/>
                <w:right w:val="single"/>
                <w:insideH w:val="single"/>
                <w:insideV w:val="single"/>
            </w:tblBorders>
        </w:tblPr>
        <w:tr>
            <w:tc>
                <w:p>
                    <w:r>
                        <w:t>a</w:t>
                    </w:r>
                    <w:r>
                        <w:t>d</w:t>
                    </w:r>
                </w:p>
                <w:p/>
            </w:tc>
            <w:tc>
                <w:p>
                    <w:r>
                        <w:t>b</w:t>
                    </w:r>
                </w:p>
            </w:tc>
        </w:tr>
    </w:tbl>
</w:body>
</w:document>


When I use the createTable Method to add a new table to a tableCell, then this method add the table to the
tablecell and to the document body.

Code:

tableOne.getRow(0).getCell(0).insertTable(0, document.createTable(1, 1));
tableOne.getRow(0).getCell(0).insertTable(0, table);

When I save this document again, the nested table is added append the first table.

Is there a way to create nested Tables with POI? Or is the a workaround?

Thanks for any help.

Felix
Attached Images

Need working example of Poi-ooxml for osgi bundle

$
0
0
Hello ,

I am working on stream based excel in order to export large excel file so we are using poi-ooxml jar . We need to create osgi bundle and we deploy the created bundle inside apache Karaf . But when I tried to wrap ooxml jar and deploy , it did not allow me to deploy my main application as it gives few dependency missing error and when I wrapped poi too , it started giving me classDefnotFoundError as it tries to find the classes in poi jar itself while it should actually search in poi-ooxml jar .

Can you please help me with the working example . It would be a big help !!!

Thanks ,
Nirmit

add a logo in header

$
0
0
hi,
can any body please help me to add a logo in header of docx.
Code:

public static void main(String[] args) {
        try {
            //XWPFDocument docx = new XWPFDocument();
            CustomXWPFDocument docx = new CustomXWPFDocument();
            CTSectPr sectPr = docx.getDocument().getBody().addNewSectPr();
            XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(docx, sectPr);
            File imgFile = new File("D:\\test.jpg");
            //write header content
            CTP ctpHeader = CTP.Factory.newInstance();
            CTR ctrHeader = ctpHeader.addNewR();
            CTText ctHeader = ctrHeader.addNewT();
            String headerText = "This is header";
            ctHeader.setStringValue(headerText);

            XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, docx);

            headerParagraph.setAlignment(ParagraphAlignment.CENTER);
            XWPFParagraph[] parsHeader = new XWPFParagraph[1];
            String blipId = headerParagraph.getDocument().addPictureData(
                    new FileInputStream(imgFile),
                    Document.PICTURE_TYPE_JPEG);

            docx.createPicture(blipId,
                    docx.getNextPicNameNumber(Document.PICTURE_TYPE_JPEG),
                    252, 200);

            parsHeader[0] = headerParagraph;
            policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader);

            //write footer content
            CTP ctpFooter = CTP.Factory.newInstance();
            CTR ctrFooter = ctpFooter.addNewR();
            CTText ctFooter = ctrFooter.addNewT();
            String footerText = "This is footer";
            ctFooter.setStringValue(footerText);
            XWPFParagraph footerParagraph = new XWPFParagraph(ctpFooter, docx);
            XWPFParagraph[] parsFooter = new XWPFParagraph[1];
            parsFooter[0] = footerParagraph;
            policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter);

            //write body content
            XWPFParagraph bodyParagraph = docx.createParagraph();
            bodyParagraph.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun r = bodyParagraph.createRun();
            r.setBold(true);
            r.setText("This is body content.");

            FileOutputStream out = new FileOutputStream("D:/a/test.docx");
            docx.write(out);
            out.close();
            System.out.println("Done");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

please check what is the issue ,the image is added in doc not in header ,
please help me

thanks in advance

excell comments in large excell files ?

$
0
0
Hello,

I am writing a large excell file using java poi version 3.9
(the file has 3500 lines). I have a problems with writing excell comments.

I mean, if write just hundred excell lines, everythihg works fine.

If I write the "real" file, with thousands of lines, the program does not give any exception/error.
But when I open the excell file, many of the comments are missing.

It seems that comments are missing after a certain point, like POI (or excell ?) is not able to manage
a large number of comments.

Any suggestions ?

Here below is my source code.

thank you
Fabio






public static void appendComment(HSSFCell aHSSFCell, String strComment)
{
String strCurrentComment = "";
String strNewCompleteComment = "";

HSSFComment aHSSFComment = aHSSFCell.getCellComment();
if(aHSSFComment != null) {
aHSSFCell.removeCellComment();
HSSFRichTextString aHSSFRichTextString = aHSSFComment.getString();
if(aHSSFRichTextString != null) {
strCurrentComment = aHSSFRichTextString.getString().trim();
}
}


if(strCurrentComment.equals("") == true) {
strNewCompleteComment = strComment;
} else {
strNewCompleteComment = strCurrentComment + "\n" + strComment;
}




CreationHelper factory = aHSSFCell.getSheet().getWorkbook().getCreationHelp er();

// When the comment box is visible, have it show in a 1x3 space
ClientAnchor anchor = factory.createClientAnchor();
anchor.setCol1(aHSSFCell.getColumnIndex());
anchor.setCol2(aHSSFCell.getColumnIndex()+5);


Drawing drawing = aHSSFCell.getSheet().createDrawingPatriarch();

anchor.setRow1(aHSSFCell.getRow().getRowNum());
anchor.setRow2(aHSSFCell.getRow().getRowNum()+3);

// Create the comment and set the text+author
Comment comment = drawing.createCellComment(anchor);


HSSFRichTextString richString = new HSSFRichTextString( strNewCompleteComment );
//richString.

//RichTextString str = factory.createRichTextString("Hello, World!");
//str.getString()

comment.setString(richString);
comment.setAuthor("Apache POI");

//comment.getString()

// Assign the comment to the cell
aHSSFCell.setCellComment(comment);
}
Viewing all 120 articles
Browse latest View live