Changeset 7678


Ignore:
Timestamp:
Mar 27, 2019, 1:57:51 PM (3 years ago)
Author:
Nicklas Nordborg
Message:

References #2161: Import data from Excel files

Implemented all print(), println() and write() methods in the XlsxTableWriter to make it almost a drop-in replacement for any PrintWriter usage that want to support writing to both CSV and Excel files with the same code.

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/util/excel/XlsxTableWriter.java

    r7675 r7678  
    2626 
    2727  * The  {@link #tablePrintHeaders(Object...)} and {@link #tablePrintData(Object...)}
    28     (including overloaded variants) methods are the only methods
    29     that can be used. None of the "print" or "write" methods have been implemented
    30     and will simply be ignored.
     28    (including overloaded variants) methods can be used to write a single row at
     29    a time. The various print() and write() methods write to a single cell and
     30    will advance one column at a time. The println() variants ends the current row
     31    and starts over on the first column.
    3132   
    3233  * Writing data to Excel is by default handled by the {@link AutoFormatter} implementation
     
    5556  private int rowNo;
    5657  private Row currentRow;
     58  private int colNo;
    5759 
    5860  /**
     
    146148  {
    147149    currentRow = sheet.createRow(rowNo++);
     150    colNo = 0;
    148151    String nv = getNullValue();
    149152    if (formatters == null) formatters = Collections.emptyMap();
     
    180183  }
    181184 
     185  @Override
     186  public void write(int c)
     187  {
     188    writeData(c, false);
     189  }
     190
     191  @Override
     192  public void write(char[] buf, int off, int len)
     193  {
     194    writeData(String.copyValueOf(buf, off, len), false);
     195  }
     196
     197  @Override
     198  public void write(char[] buf)
     199  {
     200    write(buf, 0, buf.length);
     201  }
     202
     203  @Override
     204  public void write(String s, int off, int len)
     205  {
     206    writeData(s.substring(off, off+len), false);
     207  }
     208
     209  @Override
     210  public void write(String s)
     211  {
     212    writeData(s, false);
     213  }
     214
     215  @Override
     216  public void print(boolean b)
     217  {
     218    writeData(b, false);
     219  }
     220
     221  @Override
     222  public void print(char c)
     223  {
     224    writeData(c, false);
     225  }
     226
     227  @Override
     228  public void print(int i)
     229  {
     230    writeData(i, false);
     231  }
     232
     233  @Override
     234  public void print(long l)
     235  {
     236    writeData(l, false);
     237  }
     238
     239  @Override
     240  public void print(float f)
     241  {
     242    writeData(f, false);
     243  }
     244
     245  @Override
     246  public void print(double d)
     247  {
     248    writeData(d, false);
     249  }
     250
     251  @Override
     252  public void print(char[] s)
     253  {
     254    writeData(String.copyValueOf(s), false);
     255  }
     256
     257  @Override
     258  public void print(String s)
     259  {
     260    writeData(s, false);
     261  }
     262
     263  @Override
     264  public void print(Object obj)
     265  {
     266    writeData(obj, false);
     267  }
     268
     269  @Override
     270  public void println()
     271  {
     272    if (colNo == 0) rowNo++;
     273    colNo = 0;
     274  }
     275
     276  @Override
     277  public void println(boolean b)
     278  {
     279    writeData(b, true);
     280  }
     281
     282  @Override
     283  public void println(char c)
     284  {
     285    writeData(c, true);
     286  }
     287
     288  @Override
     289  public void println(int i)
     290  {
     291    writeData(i, true);
     292  }
     293
     294  @Override
     295  public void println(long l)
     296  {
     297    writeData(l, true);
     298  }
     299
     300  @Override
     301  public void println(float f)
     302  {
     303    writeData(f, true);
     304  }
     305
     306  @Override
     307  public void println(double d)
     308  {
     309    writeData(d, true);
     310  }
     311
     312  @Override
     313  public void println(char[] s)
     314  {
     315    writeData(String.copyValueOf(s), true);
     316  }
     317
     318  @Override
     319  public void println(String s)
     320  {
     321    writeData(s, true);
     322  }
     323
     324  @Override
     325  public void println(Object o)
     326  {
     327    writeData(o, true);
     328  }
     329
     330  /**
     331    Write the data to the next cell. If colNo=0 a new row is created,
     332    otherwise the next cell in the current row is used. If
     333    'endOfRow' the current row is ended (by setting colNo to 0) so
     334    that a new row is created the next time this method is called.
     335  */
     336  @SuppressWarnings({ "rawtypes", "unchecked" })
     337  private void writeData(Object data, boolean endOfRow)
     338  {
     339    if (colNo == 0) currentRow = sheet.createRow(rowNo++);
     340    Formatter fmt = getColumnFormatter(colNo);
     341    ExcelFormatter excelFmt = (fmt instanceof ExcelFormatter) ? (ExcelFormatter)fmt : autoFormatter;
     342    ExcelValue ev = excelFmt.toExcelValue(data);
     343    Cell cell = currentRow.createCell(colNo++);
     344    ev.writeTo(cell, styleCreator);
     345    if (endOfRow) colNo = 0;
     346  }
     347 
    182348}
  • trunk/src/test/TestExcelFunctions.java

    r7677 r7678  
    9595      headerStyle.setFont(bold);
    9696
    97       CellStyle dataStyle = styleCreator.createStyle();
     97      CellStyle commentStyle = styleCreator.createStyle();
    9898      Font greyItalic = styleCreator.createFont();
    9999      greyItalic.setItalic(true);
    100100      greyItalic.setColor(IndexedColors.GREY_50_PERCENT.getIndex());
    101       dataStyle.setFont(greyItalic);
     101      commentStyle.setFont(greyItalic);
    102102     
    103103      Object[] data = new Object[7];
     
    139139      data[6] = "Column formatter line";
    140140
    141       styleCreator.setBaseStyle(dataStyle);
    142141      tw.tablePrintData(data);
     142     
     143      tw.println();
     144      tw.print("Printed");
     145      tw.print(3);
     146      tw.print(555);
     147      tw.print(532.678f);
     148      tw.print(today);
     149      tw.print(now);
     150      tw.println("Printed line");
     151      styleCreator.setBaseStyle(commentStyle);
     152      tw.print("#End-of-file");
     153      tw.print((Object)null);
     154      tw.print("<-There should be an empty cell here");
    143155     
    144156      out = FileUtil.getOutputStream(new java.io.File(outFile));
Note: See TracChangeset for help on using the changeset viewer.