java 读取模板EXCEL写入数值

来源:互联网 发布:prae是什么软件 编辑:程序博客网 时间:2024/05/21 06:27

自己写了个读取excel模板的写入数据的列子

package copy.user;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.sql.Connection;import java.sql.ResultSet;import java.sql.Statement;import java.util.ArrayList;import java.util.List;import jxl.Cell;import jxl.Sheet;import jxl.Workbook;import jxl.write.Label;import jxl.write.WritableCellFormat;import jxl.write.WritableSheet;import jxl.write.WritableWorkbook;import jxl.write.WriteException;public class ExcelMain {/** *  * 这个是单纯读取EXCEL数据 * */public  void read() {StringBuffer sb = new StringBuffer();          Workbook wb = null;           try {           File is = new File("f:\\ExcelTest\\try.xls");            // 获取工作簿对象               wb = Workbook.getWorkbook(is);               if (wb != null) {                   // 获取工作簿对象就可以获取工作簿内的工作表对象                   Sheet[] sheets = wb.getSheets();                   if (sheets != null && sheets.length != 0) {                       // 遍历工作簿内所有工作表                       for (int i=0;i<sheets.length;i++) {                           // 获取该工作表内的行数                           int rows = sheets[i].getRows();                           // 遍历行                           for (int j=0;j<rows;j++) {                           String cell ="";                            // 获取当前行的所有单元格                               Cell[] cells = sheets[i].getRow(j);                               if (cells != null && cells.length != 0) {                                   // 遍历单元格                                   for (int k=0;k<cells.length;k++) {                                       // 获取当前单元格的值                                   if(!cells[k].getContents().equals("")){                                cell += cells[k].getContents();                                   }                                    // 缩进                                       sb.append(cell + "\t");                                   }                                   System.out.println(cell);                                sb.append("\t\n");                               }                           }                           sb.append("\t\n");                       }                   }                   System.out.println("成功读取了:" +is.getName()+ "\n");               }           } catch (Exception e) {               e.printStackTrace();        } finally {           //关闭            wb.close();           }   }/** *  * 这是单纯的写EXCEL表格 * **/private void write(){WritableWorkbook wwb = null;           Label label = null;           String file ="f:\\ExcelTest\\3.xls";        try {               // 创建可写入的工作簿对象               wwb = Workbook.createWorkbook(new File(file));               if (wwb != null) {                   // 在工作簿里创建可写入的工作表,第一个参数为工作表名,第二个参数为该工作表的所在位置             WritableSheet ws = wwb.createSheet("Sheet4", 2);                   if (ws != null) {                       /* 添加表结构 */                      // 行                       for (int i=0;i<5;i++) {                           // 列                           for (int j=0;j<5;j++) {                               // Label构造器中有三个参数,第一个为列,第二个为行,第三个则为单元格填充的内容                               label = new Label(j, i, "第"+(i+1)+"行," + "第"+(j+1)+"列");                               // 将被写入数据的单元格添加到工作表                               ws.addCell(label);                           }                       }                       // 从内存中写入到文件                       wwb.write();                   }                   System.out.println("路径为:" + file + "的工作簿写入数据成功!");               }           } catch (Exception e) {               System.out.println(e.getMessage());           } finally {               try { wwb.close();} catch (WriteException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}           }   }/** *  * 这个是读取模板写入数据 * **/private void readWriter(){WritableWorkbook wwb=null;WritableSheet wws=null;FileOutputStream out =null;Connection conn=null;Statement st=null;ResultSet mySQLRS=null;//获取要读取的EXCEL表格模板File is = new File("f:\\ExcelTest\\try.xls");        String filename="f:\\ExcelTest\\";        //写入到新的表格里        File f=new File(filename,"try1.xls");        try {        //创建新文件        f.createNewFile();        out = new FileOutputStream(f);        //获取工作簿对象   Workbook wb = Workbook.getWorkbook(is);// 创建可写入的工作簿对象  wwb = Workbook.createWorkbook(out, wb);//根据工作表名获取WritableSheet对象wws=wwb.getSheet("Sheet1");//这个是链接数据库的一个对象CopyUser cp=new CopyUser();//获取connectionconn=cp.getMYConnection();//创建Statementst=conn.createStatement();WritableCellFormat wcf=new WritableCellFormat(); //设置样式wcf.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN); mySQLRS=st.executeQuery("select name,officeName from mix_end_user where officeName like '河南%' ");Label label=null;List<String> list=new ArrayList<String>();while(mySQLRS.next()){//将数据存放在List当中list.add(mySQLRS.getString("name"));}int count_02=0;for(int i=3;i<24;i++){for(int j=3;j<9;j++){//创建label对象设置value值j相当于是X轴I是Y轴位置label= new Label(j,i,list.get(count_02),wcf);//添加到工作薄中wws.addCell(label);count_02++;}}//将新建立的工作薄写入到磁盘wwb.write();        } catch (Exception e) {e.printStackTrace();} finally{//关闭流try {wwb.close();out.close();mySQLRS.close();st.close();conn.close();} catch (Exception e) {e.printStackTrace();}}}public static void main(String[] args) {new ExcelMain().readWriter();}}


0 0
原创粉丝点击