Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 保存excel文件_Java_Excel_Apache_File_Save - Fatal编程技术网

Java 保存excel文件

Java 保存excel文件,java,excel,apache,file,save,Java,Excel,Apache,File,Save,我编写了一个程序,使用ApachePOI3.9将一些数据(数据只是存储在字符串数组s[]中的四个数字)保存到excel文件中。 第二个要求是在程序运行时将数据附加到同一个文件中。在执行此操作时,我注意到只有在每次执行程序后物理打开并保存文件时,追加才会成功(这是因为只有在保存文件时才会更新行数,并且只有更新的行数才有助于成功追加操作) 所以我的问题是,有没有办法用程序本身保存文件 /* * To change this license header, choose License Heade

我编写了一个程序,使用ApachePOI3.9将一些数据(数据只是存储在字符串数组s[]中的四个数字)保存到excel文件中。 第二个要求是在程序运行时将数据附加到同一个文件中。在执行此操作时,我注意到只有在每次执行程序后物理打开并保存文件时,追加才会成功(这是因为只有在保存文件时才会更新行数,并且只有更新的行数才有助于成功追加操作) 所以我的问题是,有没有办法用程序本身保存文件

 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package webcrawler;

import java.util.*;
import java.io.*;
import java.net.URL;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;


import org.apache.poi.xssf.usermodel.*;



/**
 *
 * @author sreekar
 */
public class WebCrawler {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
           try
       {

       int val=0;
       ArrayList <String>addr=new ArrayList<String>();
       addr.add("http://172.26.50.67:8192/0");
       addr.add("http://172.26.50.67:8192/1");
       addr.add("http://172.26.50.67:8192/2");
       addr.add("http://172.26.50.67:8192/3");

       ArrayList <String> files=new ArrayList<String>();
       files.add("C://Users//sreekar//Desktop//node0.xlsx");
       files.add("C://Users//sreekar//Desktop//node1.xlsx");
       files.add("C://Users//sreekar//Desktop//node2.xlsx");
       files.add("C://Users//sreekar//Desktop//node3.xlsx");




             for(val=0;val<addr.size();val++)
             {
                         URL my_url = new URL(addr.get(val));
             System.out.println(my_url);
             BufferedReader br = new BufferedReader(new InputStreamReader(my_url.openStream()));
             String temp=br.readLine();
             Scanner  sc=new Scanner(temp);
                         String [] s=new String[4];
                         for(int i=0;i<s.length;i++)
                         {
                             s[i]=sc.next();
                         }
             XSSFWorkbook wb = null;
                         XSSFSheet sheet = null;
                         XSSFCell cell=null;
                         XSSFRow row=null;

                         File f=new File(files.get(val));
                         if(f.exists())
                        {
                             FileInputStream input_doc = new FileInputStream(f); 
                             try {
                                 wb = new XSSFWorkbook(input_doc);
                             } catch (IOException iOException) 
                             {
                              iOException.printStackTrace();
                             }
                             sheet=wb.getSheet("node " + val);
                             int rowcount=sheet.getPhysicalNumberOfRows();
                             row = sheet.createRow(rowcount++);
                             for (int c=0;c<s.length;c++) {
                                 cell = row.createCell(c);
                                 cell.setCellValue(s[c]);
                             }
                             input_doc.close();

                         }

                        else
                        {
                           int rowcount=0;
                           wb=new XSSFWorkbook();
                           sheet= wb.createSheet("node " + val);
                           row = sheet.createRow(rowcount++);
                           for(int c = 0 ; c < s.length; c++)
                              {
                                cell = row.createCell(c);                    
                                cell.setCellValue(s[c]);
                              }
                        }

                        FileOutputStream out;
                        out = new FileOutputStream(f,true);
                        wb.write(out);
                        out.close();
                    }
           }
        catch (Exception e)
       {
              e.printStackTrace();
           }
        }
}
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
包装包装;
导入java.util.*;
导入java.io.*;
导入java.net.URL;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入org.apache.poi.xssf.usermodel.*;
/**
*
*@作者sreekar
*/
公共类网络爬虫器{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args)抛出FileNotFoundException、IOException{
尝试
{
int-val=0;
ArrayList addr=新的ArrayList();
地址添加(“http://172.26.50.67:8192/0");
地址添加(“http://172.26.50.67:8192/1");
地址添加(“http://172.26.50.67:8192/2");
地址添加(“http://172.26.50.67:8192/3");
ArrayList files=新的ArrayList();
添加(“C://Users//sreekar//Desktop//node0.xlsx”);
添加(“C://Users//sreekar//Desktop//node1.xlsx”);
添加(“C://Users//sreekar//Desktop//node2.xlsx”);
添加(“C://Users//sreekar//Desktop//node3.xlsx”);

对于(val=0;val)你能详细说明一下吗?你能详细说明一下吗?