最近在做一个用java API操作Excel的程序,想把Excel的内容读出来后,进行更改,比如说我读了一个单元格的内容,我想直接把它更改了。但是这样好像不行呀!必须要创建一个 WritableWorkbook的类才可以进行更改,请问我不要WritableWorkbook类,直接把读出来的内容进行更改可以吗?

解决方案 »

  1.   

    用 POI 吧,方便得多,不需要区分什么读写 Workbook 的。
      

  2.   

    楼主用的应该是jxl吧.这个东西我也用过,它所谓的修改,其实就是重新对单元格的内容进行写入,和你第一次写入单元格内容是一样的操作,所以必须用到WritableWorkbook类.
      

  3.   

    对的 
    jxl必须这样的
    jxl其实比PIO容易学
      

  4.   

    去下个jxl.jar
    可以生成Excel的
      

  5.   

    貌似现在poi更强大些
    打算尝试
      

  6.   

    下载一个poi  传递一个jtable  就直接打印了package com.fox_ice.TestDB;import java.awt.*;
    import javax.swing.*;
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFRichTextString;
    import javax.swing.table.TableModel;
    import org.apache.poi.hssf.util.HSSFColor;
    import org.apache.poi.hssf.usermodel.HSSFFont;
    import java.io.IOException;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFCellStyle;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import java.io.FileOutputStream;public class Ipexcel
        extends JFrame {
      JPanel contentPane;
      BorderLayout borderLayout1 = new BorderLayout();
      JTable jtable;
      public Ipexcel(JTable jtable) {
        try {
          this.jtable=jtable;
          setDefaultCloseOperation(EXIT_ON_CLOSE);
          jbInit();
        }
        catch (Exception exception) {
          exception.printStackTrace();
        }
      }  /**
       * Component initialization.
       *
       * @throws java.lang.Exception
       */
      private void jbInit() throws Exception {
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(borderLayout1);
        setSize(new Dimension(400, 300));
        setTitle("Frame Title");
        FileDialog fd=new FileDialog(this,"选择保存文件的地址",
                                FileDialog.SAVE);
     fd.setFile("这里修改文件名.xls");
      fd.show();
    String strFile=fd.getDirectory()+fd.getFile();
    /* JFileChooser chooser = new JFileChooser(".");
    File s=chooser.getCurrentDirectory() ;
    String s1=s+"";
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    JavaFileFilter javaFilter = new JavaFileFilter();
    chooser.addChoosableFileFilter(javaFilter);
    chooser.setFileFilter(javaFilter);
    int rs = chooser.showSaveDialog(Main.this); */
    FileOutputStream fos = new FileOutputStream(strFile);
    //    JFileChooser jfc=new JFileChooser();       HSSFWorkbook wb=new HSSFWorkbook();
           HSSFSheet hs=wb.createSheet();
           TableModel tm=jtable.getModel();
           int row=tm.getRowCount();
           int cloumn=tm.getColumnCount();
           HSSFCellStyle style=wb.createCellStyle();
           style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
           style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
           style.setBorderRight(HSSFCellStyle.BORDER_THIN);
           style.setBorderTop(HSSFCellStyle.BORDER_THIN);
           style.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
           style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
           HSSFFont font = wb.createFont();
           font.setFontHeightInPoints((short)11);
           style.setFont(font);
           HSSFCellStyle style1=wb.createCellStyle();
           style1.setBorderBottom(HSSFCellStyle.BORDER_THIN);
           style1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
           style1.setBorderRight(HSSFCellStyle.BORDER_THIN);
           style1.setBorderTop(HSSFCellStyle.BORDER_THIN);
           style1.setFillForegroundColor(HSSFColor.ORANGE.index);
           style1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
           HSSFFont font1 = wb.createFont();
           font1.setFontHeightInPoints((short)15);
           font1.setBoldweight((short)700);
           style1.setFont(font);       for(int i=0;i<row+1;i++)
           {
                HSSFRow hr=hs.createRow(i);
                for(int j=0;j<cloumn;j++)
                {
                     if(i==0)
                     {
                          String value=tm.getColumnName(j);
                          int len=value.length();
                          hs.setColumnWidth((short)j,(short)(len*400));
                          HSSFRichTextString srts=new HSSFRichTextString(value);
                          HSSFCell hc=hr.createCell((short)j);
          //                hc.setEncoding((short)1);
                          hc.setCellStyle(style1);
                          hc.setCellValue(srts);
                     }
                     else
                     {
                       String value = "";
                       if ( (String) tm.getValueAt(i - 1, j) == null) {
                         value = "";
                       }
                       else {
                         value = tm.getValueAt(i - 1, j).toString();
                       }                      HSSFRichTextString srts=new HSSFRichTextString(value);
                          HSSFCell hc=hr.createCell((short)j);
     //                     hc.setEncoding((short)1);
                          hc.setCellStyle(style);                      if(value.equals("")||value==null)
                          {
                                hc.setCellValue(new HSSFRichTextString(""));
                          }
                          else
                          {
                                hc.setCellValue(srts);
                          }
                      }
                   }
               }           try
               {
                      wb.write(fos);
                      fos.close();
               }
               catch (IOException ex)
               {
                      ex.printStackTrace();
               }
      }
    }
      

  7.   

    下载一个poi  传递一个jtable  就直接打印了package com.fox_ice.TestDB;import java.awt.*;
    import javax.swing.*;
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFRichTextString;
    import javax.swing.table.TableModel;
    import org.apache.poi.hssf.util.HSSFColor;
    import org.apache.poi.hssf.usermodel.HSSFFont;
    import java.io.IOException;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFCellStyle;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import java.io.FileOutputStream;public class Ipexcel
        extends JFrame {
      JPanel contentPane;
      BorderLayout borderLayout1 = new BorderLayout();
      JTable jtable;
      public Ipexcel(JTable jtable) {
        try {
          this.jtable=jtable;
          setDefaultCloseOperation(EXIT_ON_CLOSE);
          jbInit();
        }
        catch (Exception exception) {
          exception.printStackTrace();
        }
      }  /**
       * Component initialization.
       *
       * @throws java.lang.Exception
       */
      private void jbInit() throws Exception {
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(borderLayout1);
        setSize(new Dimension(400, 300));
        setTitle("Frame Title");
        FileDialog fd=new FileDialog(this,"选择保存文件的地址",
                                FileDialog.SAVE);
     fd.setFile("这里修改文件名.xls");
      fd.show();
    String strFile=fd.getDirectory()+fd.getFile();
    /* JFileChooser chooser = new JFileChooser(".");
    File s=chooser.getCurrentDirectory() ;
    String s1=s+"";
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    JavaFileFilter javaFilter = new JavaFileFilter();
    chooser.addChoosableFileFilter(javaFilter);
    chooser.setFileFilter(javaFilter);
    int rs = chooser.showSaveDialog(Main.this); */
    FileOutputStream fos = new FileOutputStream(strFile);
    //    JFileChooser jfc=new JFileChooser();       HSSFWorkbook wb=new HSSFWorkbook();
           HSSFSheet hs=wb.createSheet();
           TableModel tm=jtable.getModel();
           int row=tm.getRowCount();
           int cloumn=tm.getColumnCount();
           HSSFCellStyle style=wb.createCellStyle();
           style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
           style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
           style.setBorderRight(HSSFCellStyle.BORDER_THIN);
           style.setBorderTop(HSSFCellStyle.BORDER_THIN);
           style.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
           style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
           HSSFFont font = wb.createFont();
           font.setFontHeightInPoints((short)11);
           style.setFont(font);
           HSSFCellStyle style1=wb.createCellStyle();
           style1.setBorderBottom(HSSFCellStyle.BORDER_THIN);
           style1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
           style1.setBorderRight(HSSFCellStyle.BORDER_THIN);
           style1.setBorderTop(HSSFCellStyle.BORDER_THIN);
           style1.setFillForegroundColor(HSSFColor.ORANGE.index);
           style1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
           HSSFFont font1 = wb.createFont();
           font1.setFontHeightInPoints((short)15);
           font1.setBoldweight((short)700);
           style1.setFont(font);       for(int i=0;i<row+1;i++)
           {
                HSSFRow hr=hs.createRow(i);
                for(int j=0;j<cloumn;j++)
                {
                     if(i==0)
                     {
                          String value=tm.getColumnName(j);
                          int len=value.length();
                          hs.setColumnWidth((short)j,(short)(len*400));
                          HSSFRichTextString srts=new HSSFRichTextString(value);
                          HSSFCell hc=hr.createCell((short)j);
          //                hc.setEncoding((short)1);
                          hc.setCellStyle(style1);
                          hc.setCellValue(srts);
                     }
                     else
                     {
                       String value = "";
                       if ( (String) tm.getValueAt(i - 1, j) == null) {
                         value = "";
                       }
                       else {
                         value = tm.getValueAt(i - 1, j).toString();
                       }                      HSSFRichTextString srts=new HSSFRichTextString(value);
                          HSSFCell hc=hr.createCell((short)j);
     //                     hc.setEncoding((short)1);
                          hc.setCellStyle(style);                      if(value.equals("")||value==null)
                          {
                                hc.setCellValue(new HSSFRichTextString(""));
                          }
                          else
                          {
                                hc.setCellValue(srts);
                          }
                      }
                   }
               }           try
               {
                      wb.write(fos);
                      fos.close();
               }
               catch (IOException ex)
               {
                      ex.printStackTrace();
               }
      }
    }