如题,能给导入导出表成excel的例子最好。发我邮箱也行—— [email protected]   (问题解决后马上给分)

解决方案 »

  1.   

    要excel能通过java程序导入数据库,也要能从数据库导出生成到excel中。
    那位前辈能给我个例子,越完整越好!
      

  2.   


    可以使用第三方工具,例如MySQL 可以用Navicat for MySQL SQL SERVER 自身就带有这样的工具
      

  3.   

    看一下这哥们儿的代码,也许你就能做了http://yongsky.javaeye.com/blog/99075
      

  4.   

    在网上查到可以用POI和JXL实现。
    谁能给个用JXL实现操作EXCEL出入数据库的例子么?
      

  5.   

    这个需要一个包:专门把DB中数据导入EXCEL,下面是我以前做过的一个项目代码你卡看
    package emanagement;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import java.sql.*;
    import javax.swing.JFileChooser;
    import java.io.FileOutputStream;
    import java.io.*;
    import javax.swing.JOptionPane;
    import java.util.Vector;public class ExportExcel {
      ResultSet rs;
      public ExportExcel() {
      }
      public void export() throws IOException {
        HSSFWorkbook workbook=new HSSFWorkbook();
        HSSFSheet sheet=null;
        int k=0;
        ConnectionDB cdb=new ConnectionDB();
        rs=cdb.ExportExcel();
        if(k==0)
        {
          sheet=workbook.createSheet("入库数据");
          setInfantCellTitle(sheet);
          k++;
        }
        try {
          while (rs.next()) {
             HSSFRow row=sheet.createRow(k);
             row.createCell((short)0).setCellValue(rs.getString(2));
             row.createCell((short)1).setCellValue(rs.getString(3));
             row.createCell((short)2).setCellValue(rs.getString(4));
             row.createCell((short)3).setCellValue(rs.getString(5));
             row.createCell((short)4).setCellValue(rs.getString(6));
             row.createCell((short)5).setCellValue(rs.getString(7));
             row.createCell((short)6).setCellValue(rs.getString(8));
             row.createCell((short)7).setCellValue(rs.getString(9));
             row.createCell((short)8).setCellValue(rs.getString(10));
             row.createCell((short)9).setCellValue(rs.getString(11));
             k++;
          }
          try {
            JFileChooser jfile=new JFileChooser();
            jfile.showOpenDialog(null);
            String file=jfile.getSelectedFile().getPath();
            FileOutputStream fOut = null;
            try {
              fOut = new FileOutputStream(file);
            }
            catch (FileNotFoundException ex2) {
            }
            workbook.write(fOut);
            fOut.flush();
            fOut.close();
            JOptionPane.showMessageDialog(null, "数据导出成功!","数据导出",2);
          }
          catch (FileNotFoundException ex2) {
            ex2.printStackTrace();
          }    }
        catch (SQLException ex) {
          ex.printStackTrace();
        }finally
        {
            try {
              ConnectionDB.con.close();
              rs.close();
            }
            catch (SQLException ex1) {
              ex1.printStackTrace();
            }
        }  }
      public void  setInfantCellTitle(HSSFSheet sheet)
      {
        HSSFRow row=sheet.createRow(0);
        row.createCell((short)0).setCellValue("类型");
        row.createCell((short)1).setCellValue("类别");
        row.createCell((short)2).setCellValue("名称");
        row.createCell((short)3).setCellValue("规格/型号");
        row.createCell((short)4).setCellValue("单位");
        row.createCell((short)5).setCellValue("数量");
        row.createCell((short)6).setCellValue("入库人");
        row.createCell((short)7).setCellValue("入库时间");
        row.createCell((short)8).setCellValue("存放位置");
        row.createCell((short)9).setCellValue("备注");
      }
      public void InExport(Vector vec)throws IOException //入库统计数据导出
      {
        Vector vec1=new Vector();
        HSSFWorkbook workbook=new  HSSFWorkbook();
        HSSFSheet sheet=null;
        int k=0;
        if(k==0)
        {
          sheet=workbook.createSheet("入库统计数据");
          setInfantCellTitle1(sheet);
          k++;
        }
        int rows=vec.size();
        for(int i=0;i<rows;i++)
        {
          HSSFRow row=sheet.createRow(k);
          vec1=(Vector)vec.elementAt(i);
          for (int j = 0; j < 7; j++)
          {
            row.createCell((short)j).setCellValue(vec1.elementAt(j).toString());
          }
          k++;
        }
        try {
            JFileChooser jfile=new JFileChooser();
            jfile.showOpenDialog(null);
            String file=jfile.getSelectedFile().getPath();
            FileOutputStream fOut = null;
            try {
              fOut = new FileOutputStream(file);
            }
            catch (FileNotFoundException ex2) {
            }
            workbook.write(fOut);
            fOut.flush();
            fOut.close();
            JOptionPane.showMessageDialog(null, "数据导出成功!","数据导出",2);
          }
          catch (FileNotFoundException ex2) {
            ex2.printStackTrace();
          }  }
      public void  setInfantCellTitle1(HSSFSheet sheet)
      {
        HSSFRow row=sheet.createRow(0);
        row.createCell((short)0).setCellValue("类型");
        row.createCell((short)1).setCellValue("类别");
        row.createCell((short)2).setCellValue("名称");
        row.createCell((short)3).setCellValue("规格/型号");
        row.createCell((short)4).setCellValue("数量");
        row.createCell((short)5).setCellValue("入库时间");
        row.createCell((short)6).setCellValue("存放位置");
      }
      public void OutExport(Vector vec)throws IOException //入出统计数据导出
       {
         Vector vec1=new Vector();
         HSSFWorkbook workbook=new  HSSFWorkbook();
         HSSFSheet sheet=null;
         int k=0;
         if(k==0)
         {
           sheet=workbook.createSheet("出库统计数据");
           setInfantCellTitle2(sheet);
           k++;
         }
         int rows=vec.size();
         for(int i=0;i<rows;i++)
         {
           HSSFRow row=sheet.createRow(k);
           vec1=(Vector)vec.elementAt(i);
           for (int j = 0; j <8; j++)
           {
             row.createCell((short)j).setCellValue(vec1.elementAt(j).toString());
           }
           k++;
         }
         try {
             JFileChooser jfile=new JFileChooser();
             jfile.showOpenDialog(null);
             String file=jfile.getSelectedFile().getPath();
             FileOutputStream fOut = null;
             try {
               fOut = new FileOutputStream(file);
             }
             catch (FileNotFoundException ex2) {
             }
             workbook.write(fOut);
             fOut.flush();
             fOut.close();
             JOptionPane.showMessageDialog(null, "数据导出成功!","数据导出",2);
           }
           catch (FileNotFoundException ex2) {
             ex2.printStackTrace();
           }   }
       public void  setInfantCellTitle2(HSSFSheet sheet)
       {
         HSSFRow row=sheet.createRow(0);
         row.createCell((short)0).setCellValue("类型");
         row.createCell((short)1).setCellValue("类别");
         row.createCell((short)2).setCellValue("名称");
         row.createCell((short)3).setCellValue("规格/型号");
         row.createCell((short)4).setCellValue("数量");
         row.createCell((short)5).setCellValue("领用人");
         row.createCell((short)6).setCellValue("出库时间");
         row.createCell((short)7).setCellValue("用途");
       }
       public void CurrentExport(Vector vec)throws IOException //现存量统计数据导出
          {
            Vector vec1=new Vector();
            HSSFWorkbook workbook=new  HSSFWorkbook();
            HSSFSheet sheet=null;
            int k=0;
            if(k==0)
            {
              sheet=workbook.createSheet("现存量数据");
              setInfantCellTitle3(sheet);
              k++;
            }
            int rows=vec.size();
            for(int i=0;i<rows;i++)
            {
              HSSFRow row=sheet.createRow(k);
              vec1=(Vector)vec.elementAt(i);
              for (int j = 0; j <10; j++)
              {
                row.createCell((short)j).setCellValue(vec1.elementAt(j).toString());
              }
              k++;
            }
            try {
                JFileChooser jfile=new JFileChooser();
                jfile.showOpenDialog(null);
                String file=jfile.getSelectedFile().getPath();
                FileOutputStream fOut = null;
                try {
                  fOut = new FileOutputStream(file);
                }
                catch (FileNotFoundException ex2) {
                }
                workbook.write(fOut);
                fOut.flush();
                fOut.close();
                JOptionPane.showMessageDialog(null, "数据导出成功!","数据导出",2);
              }
              catch (FileNotFoundException ex2) {
                ex2.printStackTrace();
              }      }
          public void  setInfantCellTitle3(HSSFSheet sheet)
          {
            HSSFRow row=sheet.createRow(0);
            row.createCell((short)0).setCellValue("类型");
            row.createCell((short)1).setCellValue("类别");
            row.createCell((short)2).setCellValue("名称");
            row.createCell((short)3).setCellValue("规格/型号");
            row.createCell((short)4).setCellValue("单位");
            row.createCell((short)5).setCellValue("数量");
            row.createCell((short)6).setCellValue("入库人");
            row.createCell((short)7).setCellValue("入库时间");
            row.createCell((short)8).setCellValue("位置");
            row.createCell((short)9).setCellValue("备注");
          }}
      

  6.   

    /*---------------以下为创建excel文件----------------*/
    package  test; // 生成Excel的类 
     import  java.io.File; import  jxl.Workbook;
     import  jxl.write.Label;
     import  jxl.write.WritableSheet;
     import  jxl.write.WritableWorkbook; public   class  CreateExcel   {
         public   static   void  main(String args[])   {
             try    {
                 //  打开文件 
                 WritableWorkbook book  =  Workbook.createWorkbook( new  File( " test.xls " ));
                 //  生成名为“第一页”的工作表,参数0表示这是第一页 
                 WritableSheet sheet  =  book.createSheet( " 第一页 " ,  0 );
                 //  在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
                 //  以及单元格内容为test 
                 Label label  =   new  Label( 0 ,  0 ,  " test " );             //  将定义好的单元格添加到工作表中 
                 sheet.addCell(label);             /**/ /* 
                 * 生成一个保存数字的单元格 必须使用Number的完整包路径,否则有语法歧义 单元格位置是第二列,第一行,值为789.123
                  */ 
                jxl.write.Number number  =   new  jxl.write.Number( 1 ,  0 ,  555.12541 );
                sheet.addCell(number);             //  写入数据并关闭文件 
                 book.write();
                book.close();        }   catch  (Exception e)   {
                System.out.println(e);
            } 
        } 
    } /*-----------------以下为读excel文件----------------*/
    package  test; // 读取Excel的类 
     import  java.io.File; import  jxl.Cell;
     import  jxl.Sheet;
     import  jxl.Workbook; public   class  ReadExcel   {
         public   static   void  main(String args[])   {
             try    {
                Workbook book  =  Workbook.getWorkbook( new  File( " test.xls " ));
                 //  获得第一个工作表对象 
                 Sheet sheet  =  book.getSheet( 0 );
                 //  得到第一列第一行的单元格 
                 Cell cell1  =  sheet.getCell( 0 ,  0 );
                String result  =  cell1.getContents();
                System.out.println(result);
                book.close();
            }   catch  (Exception e)   {
                System.out.println(e);
            } 
        } 
    } /*---------------package  test; // 读取Excel的类 
     import  java.io.File; import  jxl.Cell;
     import  jxl.Sheet;
     import  jxl.Workbook; public   class  ReadExcel   {
         public   static   void  main(String args[])   {
             try    {
                Workbook book  =  Workbook.getWorkbook( new  File( " test.xls " ));
                 //  获得第一个工作表对象 
                 Sheet sheet  =  book.getSheet( 0 );
                 //  得到第一列第一行的单元格 
                 Cell cell1  =  sheet.getCell( 0 ,  0 );
                String result  =  cell1.getContents();
                System.out.println(result);
                book.close();
            }   catch  (Exception e)   {
                System.out.println(e);
            } 
        } 

    /*----------以下是修改excel文件--------*/
    package  test; import  java.io.File; import  jxl.Workbook;
     import  jxl.write.Label;
     import  jxl.write.WritableSheet;
     import  jxl.write.WritableWorkbook; public   class  UpdateExcel   {
         public   static   void  main(String args[])   {
             try    {
                 //  Excel获得文件 
                 Workbook wb  =  Workbook.getWorkbook( new  File( " test.xls " ));
                 //  打开一个文件的副本,并且指定数据写回到原文件 
                 WritableWorkbook book  =  Workbook.createWorkbook( new  File( " test.xls " ),
                        wb);
                 //  添加一个工作表 
                 WritableSheet sheet  =  book.createSheet( " 第二页 " ,  1 );
                sheet.addCell( new  Label( 0 ,  0 ,  " 第二页的测试数据 " ));
                book.write();
                book.close();
            }   catch  (Exception e)   {
                System.out.println(e);
            } 
        } 

     
      

  7.   

    谢谢各位的关注。
    请问谁有JXL读写excel出入数据库的完整例子啊?!(传到我邮箱也可以。)
      

  8.   

    谢谢各位前辈的关注。
    我花了2天时间学习了JXL,问题解决了。
    有需要的朋友们可以联系我,我写出了完整的jxl操作excel出入数据库的简单例子。
    结贴咯,辛苦大家了。呵呵。