poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ORANSKY, JEFF" <jo8...@att.com>
Subject Poi HSLF cell border problem code snippet
Date Wed, 29 Feb 2012 19:37:35 GMT
This causes the collapsed table when more than one cell border is added.

import java.awt.Color;
import java.awt.Dimension;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.poi.hslf.model.Line;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.Table;
import org.apache.poi.hslf.model.TableCell;
import org.apache.poi.hslf.model.TextBox;
import org.apache.poi.hslf.model.TextShape;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.usermodel.SlideShow;

public class borderTest {

        public borderTest(SlideShow ppt) {

                ppt.setPageSize(new Dimension(720,450));
                ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();
                Slide theSlide = ppt.createSlide();
                ArrayList<String> topDataElement = new ArrayList<String>();

                topDataElement.add("100");
                topDataElement.add("200");
                topDataElement.add("300");
                topDataElement.add("400");
                topDataElement.add("500");
                topDataElement.add("600");
                topDataElement.add("700");
                topDataElement.add("800");

                data.add(topDataElement);

                for (int i = 0;i < 10; i++) {
                        ArrayList<String> thisRow = new ArrayList<String>();
                        thisRow.add("100");
                        thisRow.add("200");
                        thisRow.add("300");
                        thisRow.add("400");
                        thisRow.add("500");
                        thisRow.add("600");
                        thisRow.add("700");
                        thisRow.add("800");
                        data.add(thisRow);
                }

        formatTable(data, topDataElement, theSlide);

        }

        public static void main(String[] args) {
                SlideShow ppt = new SlideShow();

                borderTest theSlide = new borderTest(ppt);

                writeOutput(ppt, "c:\\download\\test.ppt");
                System.out.println("Finished");
        }


        private void formatTable(ArrayList<ArrayList<String>> data, ArrayList<String>topDataElement,
                        Slide theSlide) {

            // topDataElement.size() is the number of columns
            // data.size is the number of rows

            Table table = new Table(data.size(), topDataElement.size());
            int rowHeight = 1;
            int lastRow = data.size() - 1;
            int columnWidth = 70;
            int lastColumn = topDataElement.size() - 1;
            int pctChgColumn = lastColumn - 3;
            int serviceNameColumn = 0;
            boolean changeColor = false;
            Line borderLine = new Line();
            borderLine.setLineColor(Color.BLACK);
            borderLine.setLineStyle(Line.LINE_SIMPLE);
            borderLine.setLineWidth(Line.DEFAULT_LINE_WIDTH);

            Iterator<ArrayList<String>> itr = data.iterator();
            int tableRow = 0;
            while (itr.hasNext()) {
                ArrayList<String> dataElement = itr.next();
                table.setRowHeight(tableRow, rowHeight);
                for (int tableColumn = 0; tableColumn < dataElement.size(); tableColumn++)
{
                        changeColor = false;

                        table.setColumnWidth(tableColumn, columnWidth);

                                TableCell cell = table.getCell(tableRow, tableColumn);
                                cell.setText(dataElement.get(tableColumn));
//                              cell.setText(utilities.splitLongString(dataElement.get(tableColumn),
33));

                                RichTextRun rt = cell.getTextRun().getRichTextRuns()[0];
                                float margin = 1.0f;
                                cell.setMarginBottom(margin);
                                cell.setMarginLeft(margin);
                                cell.setMarginRight(margin);
                                cell.setMarginTop(margin);

                                if (tableColumn == lastColumn) {
                                        if (tableRow == 1) {
                                                cell.setBorderTop(borderLine);
                                                cell.setBorderBottom(borderLine);
                                        }
//                                      if (tableRow == lastRow) {
//                                              cell.setBorderBottom(borderLine);
//                                      }
//                                      if (tableRow > 0) {
//                                              cell.setBorderLeft(borderLine);
//                                              cell.setBorderRight(borderLine);
//                                      }
                                }

//                              cell.setWordWrap(TextShape.WrapByPoints);



                                cell.setHorizontalAlignment(TextBox.AlignCenter);

                                if (tableRow < 2)
                            {
                                 rt.setBold(true);
                            }
                                else
                                {
                                        rt.setBold(false);
                                }
                                rt.setFontName("Verdana");
                            rt.setFontSize(9);
                            rt.setFontColor(Color.BLACK);

//                          if (tableColumn == serviceNameColumn) {
//                              table.setColumnWidth(tableColumn, (columnWidth * 2) + 10);
//                              cell.setHorizontalAlignment(TextBox.AlignLeft);
//                          } else
//                          {
//                                  if (tableColumn == monthlyVolumeColumn) {
//                                      table.setColumnWidth(tableColumn, columnWidth + 20);
//                                  }
//                                  else
//                                  {
//                                      table.setColumnWidth(tableColumn, columnWidth - 15);
//                                  }
//                          }


                                if (tableRow == 1) {
                                        rt.setFontSize(7);
                                        rt.setFontColor(Color.BLUE);
                                        rt.setBold(true);
                                }
                            if (tableRow == 0) {
                                rt.setFontColor(Color.white);
                                }


                            cell.setVerticalAlignment(TextBox.AnchorTop);
//                          lastColumn = tableColumn;
                } // End of for (int j = 0; j < dataElement.length; j++)
                 table.setColumnWidth(lastColumn, columnWidth + 20);
                 tableRow++;
            } // End of while (itr.hasNext())

            theSlide.addShape(table);
            // Set starting coordinates of the table
            table.moveTo(30, 70);
        } // End of formatTable method

    public static void writeOutput(SlideShow ppt, String fileName) {
        FileOutputStream out = null;

            try {
                out = new FileOutputStream(fileName);

                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                }

            try {
                        ppt.write(out);
                        out.close();
                } catch (IOException e) {
                        e.printStackTrace();
                        System.exit(1);
                }
    }  // End of writeOutput method



}

Jeff Oransky
Technical Architect, ACSI
Office: (925)901-7379
Cell: (925) 548-2786
jo8461@att.com




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Mime
View raw message