Use java extracted in Excel to calculate the average daily temperature and export

时间太紧,程序比较乱,以后用的时候注意整理调整
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class BeitunwenduCal {

 public static void main(String[] args) {
  
  String fileToBeRead="e:/beitunwendu.xls";  
  String outputFile = "e:/ btwdjsjg.xls";
  
  try {
   // 创建对Excel工作簿文件的引用  
   HSSFWorkbook reworkbook = new HSSFWorkbook(new FileInputStream(fileToBeRead));  
   // 创建对工作表的引用。  
   // 本例是按名引用(让我们假定那张表有着缺省名"Sheet1")  
   HSSFSheet resheet = reworkbook.getSheet("Sheet1");  
   // 也可用getSheetAt(int index)按索引引用,  
   // 在Excel文档中,第一张工作表的缺省索引是0,  
   // 其语句为:HSSFSheet sheet = workbook.getSheetAt(0);  
   // 读取左上端单元  
   
   HSSFWorkbook workbook = new HSSFWorkbook();
   // 在Excel工作簿中建一工作表,其名为缺省值
   // 如要新建一名为"效益指标"的工作表,其语句为:
   // HSSFSheet sheet = workbook.createSheet("效益指标");
   HSSFSheet sheet = workbook.createSheet();
   // 在索引0的位置创建行(最顶端的行)
   
   int num = resheet.getLastRowNum();
   double  dateStr =0;
   
   double wenduzhihe=0;
   int wendunum=0;
   int date1=0;
   double cellwd=0;
   for (int i = 0; i < num; i++) {
    HSSFRow rerow = resheet.getRow(i);  
    HSSFCell recell = rerow.getCell((short)0);  
    HSSFCell recell1 = rerow.getCell((short)1);  
    // 输出单元内容,cell.getStringCellValue()就是取所在单元的值  
    System.out.println("左上端单元是: " + recell.getNumericCellValue());
    
    if(!(dateStr==recell.getNumericCellValue())){
     
    if(wendunum!=0){
     HSSFRow row = sheet.createRow((short) date1);
     HSSFCell cell = row.createCell((short) 0);
     // 定义单元格为字符串类型
     cell.setCellType(HSSFCell.CELL_TYPE_STRING);
     // 在单元格中输入一些内容
     cell.setCellValue(dateStr);
     HSSFCell cell1 = row.createCell((short) 1);
     // 定义单元格为字符串类型
     cell1.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
     // 在单元格中输入一些内容
     
        cellwd=wenduzhihe/wendunum;
        
        
     cell1.setCellValue(cellwd);
     
     date1++;
    }
     dateStr=recell.getNumericCellValue();
     wenduzhihe=recell1.getNumericCellValue();
     wendunum=1;    
    }
    else{
     
     wenduzhihe=wenduzhihe+recell1.getNumericCellValue();
     wendunum++;
      
     
    }
    
    
    
   }

   FileOutputStream fOut = new FileOutputStream(outputFile);
   // 把相应的Excel 工作簿存盘
   workbook.write(fOut);
   fOut.flush();
   // 操作结束,关闭文件
   fOut.close();
   System.out.println("文件生成...");
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  
  
  
  
  
  
  

 }

}

Published 34 original articles · won praise 9 · views 90000 +

Guess you like

Origin blog.csdn.net/tianyatest/article/details/49299671