在Java语言中如何实现导入和导出Excel表,详细说下。

解决方案 »

  1.   

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.poifs.filesystem.POIFSFileSystem;//Source file: D:\\Excell.java
    public class Excell implements IExcell 
    {

    /**
     * @roseuid 45B58EFC03A9
     */
    public Excell() 
    {

    }

    /**
     * @param sheetName
     * @param path
     * @param ges
     * @return String
     * @roseuid 45B58EFC03D8
     */
    public String xie(String sheetName, String path, String[][] ges) 
    {
    // 新建文件
    HSSFWorkbook wb = new HSSFWorkbook();
    // 新建工作表
    HSSFSheet sheet = wb.createSheet(sheetName);

    for (int hang = 0; hang <ges.length; hang++)
    {
    // 创建行

    HSSFRow row = sheet.createRow((short)hang);

    for (int lie = 0; lie <ges[hang].length; lie++)
    {
    HSSFCell cell = row.createCell((short) lie);// 创建格 createCell((short) lie)
    cell.setCellValue(ges[hang][lie]);
    }
    }
    try
    {
    FileOutputStream fileout = new FileOutputStream(path);
    wb.write(fileout);
    fileout.close();
    return null;
    }
    catch (FileNotFoundException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    catch (IOException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return path;
    }


    public String[][] du(String path, String sheetName) 
    {

    try
    { POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream( path));
    // 文件
    HSSFWorkbook wb = new HSSFWorkbook(fs);
    //工作表
    HSSFSheet sheet = wb.getSheetAt(0); // 行数
    int hb = sheet.getPhysicalNumberOfRows();
    // 列数
    int lb = sheet.getRow(0).getPhysicalNumberOfCells();
    String[][] result=new String[hb][lb];
    for (int h = 0; h<hb ; h++)
    {
    // 获得工作表的行
    HSSFRow row = sheet.getRow(h); for (int l= 0;l<lb ; l++)
    {
    // 获得工作表的列
    HSSFCell cell = row.getCell((short)l);
    result[h][l]=cell.toString() ;
    }
    System.out.println();
    }
    }
    catch (FileNotFoundException e)
    {
    e.printStackTrace();
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }

    return null;
    }
    }
      

  2.   

    public interface IExcell 
    {

    /**
     * @param sheetName
     * @param path
     * @param ges
     * @return String
     * @roseuid 45B58D010138
     */
    public String xie(String sheetName, String path, String[][] ges);

    public String[][] du(String path, String sheetName);
    }
      

  3.   


    import junit.framework.TestCase;public class ExcellTest extends TestCase
    { public void testXie()
    {
    String[][] ges=
    {
    {"aa","vv"},
    {"cc","dd"}
    } ;
    IExcell k=new Excell();
    k.xie("s1", "c:/tt.xls", ges);
    } public void testDu()
    {
    IExcell a=new Excell();
    a.du("c:/tt.xls", "s1");
    }
      

  4.   

    POI我印象中是只能读不能写,楼主上google找找jxl或jexcel的实例研究一下,这个可以读也可以写的。
      

  5.   

    恩  楼上的案例 貌似还要用到jxl.jar包 
      

  6.   


       记着下载,jxl.jar包。package student;import java.io.IOException;
    import java.io.OutputStream;
    import java.sql.ResultSet;
    import java.sql.SQLException;import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;import jxl.Workbook;
    import jxl.format.Alignment;
    import jxl.format.UnderlineStyle;
    import jxl.write.Label;
    import jxl.write.WritableCellFormat;
    import jxl.write.WritableFont;
    import jxl.write.WritableSheet;
    import jxl.write.WritableWorkbook;import database.DBConn;/**
     * 学生成绩管理中,打印功能
     * @author ss
     *
     */
    public class ConvertToExcel2 extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    try {
    exportExcel(response,request);
    } catch (SQLException e) {
    e.printStackTrace();
    }
    } public void exportExcel(HttpServletResponse response,HttpServletRequest request) throws IOException,
    SQLException {






    try{
    OutputStream os = response.getOutputStream();// 取得输出流
    response.reset();// 清空输出流
    response.setCharacterEncoding("utf8");
    response.setHeader("Content-disposition", "attachment; filename="
    + "chengji" + ".xls");// 设定输出文件头
    response.setContentType("application/msexcel");// 定义输出类型 WritableWorkbook wbook = Workbook.createWorkbook(os); // 建立excel文件
    WritableSheet wsheet = wbook.createSheet("sheet1", 0); // sheet名称 // 设置excel标题
    WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16,
    WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE);
    WritableCellFormat wcfFC = new WritableCellFormat(wfont);
    wcfFC.setAlignment(Alignment.CENTRE); // 开始生成主体内容
    wsheet.addCell(new Label(0, 0, "姓名"));
    wsheet.addCell(new Label(1, 0, "学期"));
    wsheet.addCell(new Label(2, 0, "年级"));
    wsheet.addCell(new Label(3, 0, "班级科目"));
    wsheet.addCell(new Label(4, 0, "考试次数"));
    wsheet.addCell(new Label(5, 0, "成绩"));


    int i = 1;

    wsheet.addCell(new Label(0, i, "测试"));
    wsheet.addCell(new Label(1, i, "测试"));
    wsheet.addCell(new Label(2, i, "测试"));
    wsheet.addCell(new Label(3, i, "测试"));
    wsheet.addCell(new Label(4, i, "测试"));
    wsheet.addCell(new Label(5, i, "测试"));

    i++;

    rs1.close();
    db.close(); // 主体内容生成结束
    wbook.write(); // 写入文件
    wbook.close();
    os.flush();
    os.close(); // 关闭流
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    }

    }
      

  7.   

    POI 可以的
    不过 要注意03版本 和07版本 用的类不一样
      

  8.   

    我前几天写了POI的一个基本应用的blog,可以去我空间看看