关于excel怎么操作比较方面,我这里要实现一个导入功能,会把我的数据导给用户。主要是excel模板(我是swing实现的,所以希望提供java代码查考)。这个模板主要就是合并比较麻烦,另外里面的有复选框等等这些,还有一个关键是中间有些内容他的行数是不固定的,如片参考:http://zhidao.baidu.com/question/319120573.html

解决方案 »

  1.   

    有点乱 你自己整理下
    封装了一个写数据的
    一个调用格式撒的 原理就是基于模板修改数据// copy附件
    FileUtil.copyFile(from, to);
    // 准备修改 
    FileInputStream fis=new FileInputStream(to);
    POIFSFileSystem fs = new POIFSFileSystem(fis);
    HSSFWorkbook wb = new HSSFWorkbook(fs);写数据的类import java.util.List;import org.apache.poi.hssf.usermodel.HSSFAnchor;
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFCellStyle;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    public class AffixExcel {
    /*
     * 写一行分离出来 
     */
    public void setRegionRow(HSSFSheet sheet,int row,List<KpiRegionValBean> urkl,String kpiname,HSSFCellStyle cs,HSSFCellStyle cs1){
    if (row == 2) {
    HSSFRow row1 = sheet.getRow(1);
    if(row1==null)
    row1 = sheet.createRow(1);
    for (int ni = 0; ni < urkl.size(); ni++) {
    HSSFCell c1 = row1.getCell(ni + 1);
    if (c1 == null)
    c1 = row1.createCell(ni + 1);
    c1.setCellStyle(cs1);
    c1.setCellType(HSSFCell.CELL_TYPE_STRING);
    c1.setCellValue(urkl.get(ni).getRegion_name());
    }
    } HSSFRow row2 = sheet.createRow(row);
    if(row2==null)
    row2 = sheet.createRow(row);
    HSSFCell c = row2.getCell(0);
    if (c == null)
    c = row2.createCell(0);
    c.setCellStyle(cs1);
    c.setCellType(HSSFCell.CELL_TYPE_STRING);
    c.setCellValue(kpiname);
    for (int mi = 0; mi < urkl.size(); mi++) {
    c = row2.getCell(mi + 1);
    if (c == null)
    c = row2.createCell(mi + 1);
    c.setCellStyle(cs);
    c.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
    c.setCellValue(Double.valueOf(urkl.get(mi).getVal()));
    }
    }
    /*
     * 历史数据写入
     */
    public void setDateRow(HSSFSheet sheet,int rowi,List<kpiDateValBean> urkd,String kpiname,HSSFCellStyle cs,HSSFCellStyle cs1){
    if (rowi == 2) {
    HSSFRow row = sheet.getRow(1);
    if(row==null)
    row = sheet.createRow(1);
    for (int ni = 0; ni < urkd.size(); ni++) {
    HSSFCell c = row.getCell(ni + 1);
    if (c == null)
    c = row.createCell(ni + 1);
    c.setCellStyle(cs1);
    c.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
    c.setCellValue(urkd.get(ni).getDate_key()
    .substring(
    urkd.get(ni).getDate_key()
    .length() - 2));
    }
    }

    HSSFRow row2 = sheet.createRow(rowi);
    if(row2==null)
    row2 = sheet.createRow(rowi);
    HSSFCell c1 = row2.getCell(0);
    if (c1 == null)
    c1 = row2.createCell(0);
    c1.setCellStyle(cs1);
    c1.setCellType(HSSFCell.CELL_TYPE_STRING);
    c1.setCellValue(kpiname);
    for (int mi = 0; mi < urkd.size(); mi++) {
    c1 = row2.getCell(mi + 1);
    if (c1 == null)
    c1 = row2.createCell(mi + 1);
    c1.setCellStyle(cs);
    c1.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
    c1.setCellValue(Double.valueOf(urkd.get(mi).getVal()));
    }
    }

    /*
     * 写第一行的时间
     */
    public void setTime(HSSFSheet sheet,String time,HSSFCellStyle cs){
    HSSFRow row = sheet.getRow(0);
    if(row==null)
    row = sheet.createRow(0);
    HSSFCell cell = row.getCell(0);
    if (cell == null)
    cell = row.createCell(0);
    cs.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    cell.setCellStyle(cs);
    cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
    cell.setCellValue(time);
    }

    /*
     * 写第一行的名称
     */
    public void setTitle(HSSFSheet sheet,String Title,HSSFCellStyle cs){
    HSSFRow row = sheet.getRow(0);
    if(row==null)
    row = sheet.createRow(0);
    HSSFCell cell = row.getCell(0);
    if (cell == null)
    cell = row.createCell(0);
    cs.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    cell.setCellStyle(cs);
    cell.setCellType(HSSFCell.CELL_TYPE_STRING);
    cell.setCellValue(Title);
    }

    }使用的lib poi.jar 别的你自己找找吧
      

  2.   

    哥们写的参考下吧http://topic.csdn.net/u/20110830/11/b96a4b05-d775-494b-bb68-fb41e540ac9d.html含有源码下载,仔细看看帖子。
      

  3.   

    代码看不懂,有哥框架可以操作execl!
      

  4.   

    apache 的poi曹操excel还是比较方便的
      

  5.   


    哥们,进去后没有找到下载的资源,能否传我一份呢,邮箱505777612qq.com,谢谢
      

  6.   

    代码如下,可供参考package cn.ipanel.itime.util;import java.io.File;
    import java.io.FileInputStream;
    import java.util.ArrayList;
    import java.util.List;
    import org.apache.poi.hssf.usermodel.HSSFDateUtil;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class PoiExcel2 {
    public static List<String[]> read(String file, boolean flag) throws Exception {
    Workbook wb = null;
    File f = new File(file);
    FileInputStream is = new FileInputStream(f);
    if (flag) {// 2003
    wb = new HSSFWorkbook(is);
    } else {// 2007
    wb = new XSSFWorkbook(is);
    }
    return read(wb);
    } public static List<String[]> read(Workbook wb) throws Exception {
    List<String[]> list =new ArrayList<String[]>();
    try {
    for (int k = 0; k < wb.getNumberOfSheets(); k++) {  //页
    // sheet
    Sheet sheet = wb.getSheetAt(k);
    int rows = sheet.getPhysicalNumberOfRows();
    for (int r = 0; r < rows; r++) {   //行
    String str = new String();
    // 定义 row
    Row row = sheet.getRow(r);
    if (row != null) {
    int cells = row.getPhysicalNumberOfCells();

    for (short c = 0; c < cells; c++) {  //列
    Cell cell = row.getCell(c);  
    if (cell != null) {
    String value = null; switch (cell.getCellType()) { case Cell.CELL_TYPE_FORMULA:
    value = cell.getCellFormula();
    break; case Cell.CELL_TYPE_NUMERIC:
    if (HSSFDateUtil.isCellDateFormatted(cell)) {
    value =cell.getDateCellValue().toString();

    } else {
    int number = (int) cell.getNumericCellValue();
    value = number + "";
    }
    break;
    case Cell.CELL_TYPE_STRING:
    value = cell.getStringCellValue();
    break;
    case Cell.CELL_TYPE_BOOLEAN:
    value = cell.getBooleanCellValue() + "";
    cell.getDateCellValue(); break;
    default:

    }
    System.out.println(value);  
    // 第三行才开始添加数据
    if (r > 1 || r == 1) {
    //如果是最后一列,不加“ ,”
    if(c!=cells){
    str += value + ",";
    }else{
    str += value;
    }
    }
    }  //判断cell是否为空结束
    } //循环列结束
    //将数组存入List集合中,从第二列开始加到数组
    if(r>1 || r==1){
    String[] strCell = str.split(",");
    list.add(strCell);
    }
    }     //判断行是否为空结束

    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    return list;
    } public static void main(String[] args) throws Exception {
    List<String[]> list=read("f:\\历史上的今天-资料.xlsx", false);
    System.out.println("list大小:"+list.size());
    /* for(int i=0;i<list.size();i++){
    String[] c=list.get(i);
    for(int k=0;k<c.length;k++){
    System.out.println(c[k]);
    }
    }*/
    }}
    使用的是poi读取Excel数据到数据
      

  7.   

    使用poi读取Excel数据很简单,刚写过有问题要学会看api
      

  8.   

    看一下这个 http://www.360doc.com/content/11/0907/13/7396722_146444956.shtml
      

  9.   

    顶一下,不让帖子沉下去。 格式基本上可以用poi做,现在差两个功能,一个是那个下划线,一个是复选框(这个我网上找了N久没有找到),这周日没有人回答的话我就散分给大家好了,再次感谢大家的帮助。
      

  10.   

    我现在也是在用poi导出excel 但是也有问题
      

  11.   

    参考下吧http://topic.csdn.net/u/20110830/11/b96a4b05-d775-494b-bb68-fb41e540ac9d.html
      

  12.   

    jxm和poi都可以,网上一堆代码
      

  13.   

    jxl和poi都可以,网上一堆代码
      

  14.   


    下划线
    HSSFCellStyle style = book.createCellStyle();
    Font font = book.createFont();
    font.setUnderline(Font.U_SINGLE);
    style.setFont(font);
    cell.setCellStyle(style);
    combobox没找到。
    google poi combobox
    http://download.csdn.net/source/3561738源码
      

  15.   


    合并方法
                sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 1));
                sheet.addMergedRegion(new CellRangeAddress(0, 0, 2, 4));
                sheet.addMergedRegion(new CellRangeAddress(0, 0, 5, 18));
                sheet.addMergedRegion(new CellRangeAddress(0, 0, 19, 34));