当前位置: 首页 > 工具软件 > EtherCalc > 使用案例 >

Ethercalc通过excel文件overwrite

莫英喆
2023-12-01

这个方法只支持后缀名为.xls的excel文件,使用的方法是通过poi对excel文件进行解析,组成字符串,然后调用ethercalc的overwrite的api:

import java.io.File;  
import java.io.IOException;  
  
import javax.ws.rs.client.Client;  
import javax.ws.rs.client.ClientBuilder;  
import javax.ws.rs.client.Entity;  
import javax.ws.rs.core.Response;  
import javax.ws.rs.core.MediaType;  
  
import jxl.Cell;  
import jxl.Sheet;  
import jxl.Workbook;  
import jxl.read.biff.BiffException;  
import net.sf.json.JSONObject;  
  
public class overwriteByExcel {  
  
    public static void main(String args[]){  
        Client client = ClientBuilder.newClient();  
          
        String ss = readExcel("C://Users//Coffee//Desktop//new1.xls");  
        //System.out.println(ss);  
        Entity<String> payload = Entity.text(ss);  
        Response response = client.target("http://localhost:8000/_/test")  
          .request(MediaType.TEXT_PLAIN_TYPE)  
          .put(payload);  
  
        System.out.println("status: " + response.getStatus());  
        System.out.println("headers: " + response.getHeaders());  
        System.out.println("body:" + response.readEntity(String.class));  
    }  
      
    public static String readExcel(String file){    
          StringBuffer sb = new StringBuffer();    
          Workbook wb = null;    
          //构造Workbook(工作薄)对象     
          try {    
           wb = Workbook.getWorkbook(new File(file));    
          } catch (BiffException e) {    
           e.printStackTrace();    
          } catch (IOException e) {    
           e.printStackTrace();    
          }    
          if(wb == null) {    
           return null;    
          }    
          //获得了Workbook对象之后,就可以通过它得到Sheet(工作表)对象了     
          Sheet[] sheets = wb.getSheets();    
          if((sheets != null) && (sheets.length > 0)) {    
           //对每个工作表进行循环     
           for(int i=0; i<sheets.length; i++) {    
            //得到当前工作表的行数     
            int rowNum = sheets[i].getRows();    
            for(int j=0; j<rowNum; j++) {    
             //得到当前行的所有单元格     
             Cell[] cells = sheets[i].getRow(j);    
             if((cells != null) && (cells.length > 0)) {    
              //对每个单元格进行循环     
              for(int k=0; k<cells.length; k++) {    
               //读取当前单元格的值     
               String cell = cells[k].getContents();    
               if(cell.equals("33.00")){  
                   sb.append("=P7+P9"+"\t");//'\t'为Tab键     
               }else{  
                   sb.append(cell+"\t");//'\t'为Tab键     
               }                 
              }    
             }    
             sb.append("\r\n");    
            }    
            sb.append("\r\n");    
           }       
          }    
          //最后关闭资源,释放内存     
          wb.close();    
          return sb.toString();    
         }    
}  


用到的maven配置:

<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>servlet-api</artifactId>
		<version>2.5</version>
	</dependency>
<!-- https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl -->
	<dependency>
		<groupId>net.sourceforge.jexcelapi</groupId>
		<artifactId>jxl</artifactId>
		<version>2.6.12</version>
	</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
	<dependency>
		<groupId>org.apache.poi</groupId>
		<artifactId>poi</artifactId>
		<version>3.12</version>
	</dependency>


 类似资料: