您的位置:首页 > 编程语言 > Java开发

Excel学习1_Java读取文件中的内容写入excel中

2017-08-23 21:37 483 查看
public static void writeXls() {//把读取的文件写入到excel文件中

  int i = 0;

  try {

   File file = new File("d:/cn.csv");

   if (file.isFile() && file.exists()) {

    InputStreamReader read = new InputStreamReader(

      new FileInputStream(file));

    BufferedReader bufferReader = new BufferedReader(read);

    String lineTxt = null;

    String[] arg = null;

    getConnection();

    List<String[]> list = new ArrayList<String[]>() ;

    lineTxt = bufferReader.readLine();

    try {

     String tempDate;

     while (lineTxt != null && !lineTxt.trim().equals("")) {

      i++;

      arg = lineTxt.split(",");

      list.add(arg) ;

      lineTxt = bufferReader.readLine();

     }

     for(String[] args:list){

      System.out.println(args[0]+" "+args[1]+" "+args[2]+" "+args[3]);

     }

     

     operateExl(list) ;

    } catch (Exception e) {

     e.printStackTrace();

    }

   } else {

    System.out.println("文件路径不正确!");

   }

  } catch (Exception e) {

   System.out.println("文件错误!");

  } finally {

   System.out.println(i);

  }

 }

 public static void operateExl(List<String[]> list) {

  try {

   WritableWorkbook wb = Workbook.createWorkbook(new File("d:/test.xls")) ;

   WritableSheet sheet = wb.createSheet("第一页", 0) ;

   for(int i=0;i<list.size();i++){

    String arg[] = list.get(i) ;

    for(int j=0;j<arg.length;j++){

     sheet.addCell(new Label(j,i,arg[j])) ;

    }

   }

   wb.write() ;

   wb.close() ;

   } catch (Exception e) {

   // TODO: handle exception

   e.printStackTrace() ;

  }

 }

 public static void main(String[] args) {

  writeXls();

 }

原文来自:https://yq.aliyun.com/articles/48968
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐