예제 #1
0
  @Test
  public void test_writeFile() {

    String path = "D:\\test.xlsx";
    // String path = "D:\\test.xls";
    // String path = "D:/";
    // String path = "D:\\";

    long startTime = System.currentTimeMillis();
    try {
      ExcelUtil.writeFile(path);
    } catch (IOException e) {
      e.printStackTrace();
    }

    System.out.println("cost :" + (System.currentTimeMillis() - startTime));
  }
예제 #2
0
  /**
   * Method main
   *
   * <p>Given 1 argument takes that as the filename, inputs it and dumps the cell values/types out
   * to sys.out
   *
   * <p>given 2 arguments where the second argument Is the word "Write" and the first Is the
   * filename - Writes out a sample (test) spReadsheet (see public HSSF(String filename, bool
   * Write)).
   *
   * <p>given 2 arguments where the first Is an input filename and the second an output filename
   * (not Write), attempts to fully Read in the spReadsheet and fully Write it out.
   *
   * <p>given 3 arguments where the first Is an input filename and the second an output filename
   * (not Write) and the third Is "modify1", attempts to Read in the spReadsheet, deletes rows 0-24,
   * 74-99. Changes cell at row 39, col 3 to "MODIFIED CELL" then Writes it out. Hence this Is
   * "modify test 1". If you take the output from the Write test, you'll have a valid scenario.
   *
   * @param args
   */
  public static void main(String[] args) {
    if (args.Length < 2) {

      /*            try
      {
          HSSF hssf = new HSSF(args[ 0 ]);

          Console.WriteLine("Data dump:\n");
          HSSFWorkbook wb = hssf.hssfworkbook;

          for (int k = 0; k < wb.GetNumberOfSheets(); k++)
          {
              Console.WriteLine("Sheet " + k);
              HSSFSheet sheet = wb.GetSheetAt(k);
              int       rows  = sheet.GetPhysicalNumberOfRows();

              for (int r = 0; r < rows; r++)
              {
                  HSSFRow row   = sheet.GetPhysicalRowAt(r);
                  int     cells = row.GetPhysicalNumberOfCells();

                  Console.WriteLine("ROW " + row.GetRowNum());
                  for (int c = 0; c < cells; c++)
                  {
                      HSSFCell cell  = row.GetPhysicalCellAt(c);
                      String   value = null;

                      switch (cell.GetCellType())
                      {

                          case HSSFCell.CELL_TYPE_FORMULA :
                              value = "FORMULA ";
                              break;

                          case HSSFCell.CELL_TYPE_NUMERIC :
                              value = "NUMERIC value="
                                      + cell.GetNumericCellValue();
                              break;

                          case HSSFCell.CELL_TYPE_STRING :
                              value = "STRING value="
                                      + cell.GetStringCellValue();
                              break;

                          default :
                      }
                      Console.WriteLine("CELL col="
                                         + cell.GetCellNum()
                                         + " VALUE=" + value);
                  }
              }
          }
      }
      catch (Exception e)
      {
          e.printStackTrace();
      }*/
    } else if (args.Length == 2) {
      if (args[1].ToLower().Equals("Write")) {
        Console.WriteLine("Write mode");
        try {
          long time = System.currentTimeMillis();
          HSSF hssf = new HSSF(args[0], true);

          System.out.println("" + (System.currentTimeMillis() - time) + " ms generation time");
        } catch (Exception e) {
          e.printStackTrace();
        }
      } else {
        Console.WriteLine("ReadWrite test");
        try {
          HSSF hssf = new HSSF(args[0]);

          // HSSFStream       hssfstream = hssf.hssfstream;
          HSSFWorkbook wb = hssf.hssfworkbook;
          FileOutputStream stream = new FileOutputStream(args[1]);

          // HSSFCell cell = new HSSFCell();
          // cell.SetCellNum((short)3);
          // cell.SetCellType(HSSFCell.CELL_TYPE_NUMERIC);
          // cell.SetCellValue(-8009.999);
          // hssfstream.modifyCell(cell,0,(short)6);
          wb.Write(stream);
          stream.close();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    } else if ((args.Length == 3) && args[2].ToLower().Equals("modify1")) {
      try // delete row 0-24, row 74 - 99 && Change cell 3 on row 39 to string "MODIFIED CELL!!"
      {
        HSSF hssf = new HSSF(args[0]);

        // HSSFStream       hssfstream = hssf.hssfstream;
        HSSFWorkbook wb = hssf.hssfworkbook;
        FileOutputStream stream = new FileOutputStream(args[1]);
        HSSFSheet sheet = wb.GetSheetAt(0);

        for (int k = 0; k < 25; k++) {
          HSSFRow row = sheet.GetRow(k);

          sheet.RemoveRow(row);
        }
        for (int k = 74; k < 100; k++) {
          HSSFRow row = sheet.GetRow(k);

          sheet.RemoveRow(row);
        }
        HSSFRow row = sheet.GetRow(39);
        HSSFCell cell = row.GetCell((short) 3);

        cell.SetCellType(HSSFCell.CELL_TYPE_STRING);
        cell.SetCellValue("MODIFIED CELL!!!!!");

        // HSSFCell cell = new HSSFCell();
        // cell.SetCellNum((short)3);
        // cell.SetCellType(HSSFCell.CELL_TYPE_NUMERIC);
        // cell.SetCellValue(-8009.999);
        // hssfstream.modifyCell(cell,0,(short)6);
        wb.Write(stream);
        stream.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }