java - How to change font on XSSFTextBox -


xssftextbox , xssfrichtextfield's

edit see comment; apache poi seems racist, black not supported? red is?

edit 2 : instead of using xssfcolor use constants in font, have red , black; looking default fonts (just clear code correctly displays color , size now, font name/ actual font still wrong. font.color_normal works dat black)

for reason can't xssftextboxs' texts' fonts , font colors change (i assume) default calibri white (why white default?!?). sizes changed, font , font color stay defaults. have found previous bug should fixed here.

i've been looking @ this reference base how change font, , seems how it's done, doesn't seem working; i'd love set of eyes on this, there other minor problems i'm still fiddling with, font implementation biggest thing right now; criticisms welcome , wanted!

what irks me i've used xssffont xssfcellstlyes before, quite extensively, , have never had problems changing fonts or colors, let alone else, don't know if it's strange behavior i'm not seeing or if i've done incorrectly here.

code

package excelhandling;  import java.awt.desktop; import java.io.*; import javax.swing.joptionpane; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.*;  /**unit tests<br>to test random things figure them out can implement them  * in real code later; ideal test kinks here whole application  * doesn't have loaded on , on figure small issues out (living dream)  *   * @author sean newell  */ public class unittests {      static string filename = "testworkbook.xlsx";      public static void main(string[] args)     {         xssfworkbook wb = new xssfworkbook();         xssfsheet sht = wb.createsheet();         file file = new file(filename);         int colstart = 5;          xssfdrawing draw = sht.createdrawingpatriarch();          xssfshapegroup group = draw.creategroup(draw.createanchor(0, 0, 0, 0, colstart, 11, colstart + 6, 11+7));         group.setcoordinates(colstart, 11, colstart + 6, 11+7);          xssftextbox tb1 = group.createtextbox(new xssfchildanchor(0, 0, 6, 7));         tb1.setshapetype(shapetypes.rect);         tb1.setnofill(false);         tb1.setfillcolor(255, 255, 255); //this causes excel repair xml - don't know why;         //following message excel:         //repaired records: drawing /xl/drawings/drawing1.xml part (drawing shape)         //log:  //      <?xml version="1.0" encoding="utf-8" standalone="true"?> //      -<recoverylog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"> //          <logfilename>error094800_07.xml</logfilename> //          <summary>errors detected in file 'h:\my documents\netbeansprojects\templatebuilder\testworkbook.xlsx'</summary> //              -<repairedrecords summary="following list of repairs:"> //      <repairedrecord>repaired records: drawing /xl/drawings/drawing1.xml part (drawing shape)</repairedrecord> //          </repairedrecords> //      </recoverylog>          xssfrichtextstring address = new xssfrichtextstring("textbox string 1\nhas three\nlines it");          xssffont arial10 = wb.createfont();         arial10.setfontname("arial"); // doesn't seem work         arial10.setfontheight(10);         arial10.setcolor(new xssfcolor(java.awt.color.black)); // doesn't seem work         address.applyfont(arial10); // possible problem?          tb1.settext(address);         tb1.setlinestylecolor(0, 0, 0);         tb1.setlinewidth(2);           xssftextbox tb2 = group.createtextbox(new xssfchildanchor(0, 7, 10, 13));         tb2.setshapetype(shapetypes.rect);         tb2.setnofill(false);         tb2.setfillcolor(254, 254, 254); //this causes excel repair xml - don't know why         xssfrichtextstring secret = new xssfrichtextstring("a single-line thing has like, lot of text, can wrap , smaller, like, totally.");          xssffont arial8 = wb.createfont();         arial8.setfontname("arial"); // doesn't seem work         arial8.setfontheight(8);         arial8.setcolor(new xssfcolor(java.awt.color.black)); // doesn't seem work         secret.applyfont(arial8); // possible problem?          tb2.settext(secret);         tb2.setlinestylecolor(0, 0, 0);         tb2.setlinewidth(2);          try {             fileoutputstream fout;             fout = new fileoutputstream(file);              wb.write(fout);             fout.close();              joptionpane.showconfirmdialog(null, "excel sheet written");             desktop.getdesktop().open(file);          } catch (ioexception exc) {             joptionpane.showinputdialog("please close other instances of excel have " + filename + " open");         }     } } 

this quite crude, in poi 3.9 seems font family not font name copied xssffont object in xssfsimpleshape.applyattributes. furthermore set indexed colors via xssfcolor.setcolor(hssfcolor.<color>.index) - maybe there xssf-pendant hssf custom color palette, think below approach more straight-forward ...

(tested libre office 4.0, ms excel viewer, ms excel 2003 compatibility pack ...)

import java.awt.color; import java.io.*; import org.apache.poi.xssf.usermodel.*; import org.openxmlformats.schemas.drawingml.x2006.main.cttextcharacterproperties;  public class xlscolors {      static string filename = "testworkbook.xlsx";      public static void main(string[] args) throws exception {         xssfworkbook wb = new xssfworkbook();         xssfsheet sht = wb.createsheet();         file file = new file(filename);         int colstart = 5;          xssfdrawing draw = sht.createdrawingpatriarch();          xssfshapegroup group = draw.creategroup(draw.createanchor(0, 0, 0, 0, colstart, 11, colstart + 6, 11+7));         group.setcoordinates(colstart, 11, colstart + 6, 11+7);          xssftextbox tb1 = group.createtextbox(new xssfchildanchor(0, 0, 6, 7));         tb1.setlinestylecolor(0, 0, 0);         tb1.setlinewidth(2);         color col = color.orange;         tb1.setfillcolor(col.getred(), col.getgreen(), col.getblue());          xssfrichtextstring address = new xssfrichtextstring("textbox string 1\nhas three\nlines it");         tb1.settext(address);                 cttextcharacterproperties rpr = tb1.getctshape().gettxbody().getparray(0).getrarray(0).getrpr();         rpr.addnewlatin().settypeface("trebuchet ms");         rpr.setsz(900); // 9 pt         col = color.pink;         rpr.addnewsolidfill().addnewsrgbclr().setval(new byte[]{(byte)col.getred(),(byte)col.getgreen(),(byte)col.getblue()});          fileoutputstream fout = new fileoutputstream(file);         wb.write(fout);         fout.close();     } } 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -