java修改excel表格中的数据

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;


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 ChangeExcelValue {


public static void main(String[] args) {
test();
}
//转换excel中的网页大小单位为k或M
public static void test() {
//待转换文件路径
        String fileToBeRead = "D:\\vmall\\2016双十一前端性能测试\\vmall_front_result_web.xls";
        try {
            HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead));
            OutputStream os = new FileOutputStream(fileToBeRead);
            int sheetNumbers = workbook.getNumberOfSheets();
            for(int n = 0; n < sheetNumbers; n++) {
            HSSFSheet sheet = workbook.getSheetAt(n);
            //下面数组为待转换行号,从0开始
            int rows[] = new int[] {4,45,46,47};
            for(int r = 0; r < rows.length; r++) {
            HSSFRow row = sheet.getRow((short) rows[r]);
            if (null == row) {
                        continue;
                    }
            int clomns = row.getPhysicalNumberOfCells();
            //System.out.println(clomns);
           for (int i = 1; i < clomns; i++) {
                        @SuppressWarnings("deprecation")
HSSFCell cell = row.getCell((short) i);
                        String value = "";
                        if (null == cell) {
                            continue;
                        } 
                        else {
                        int type = cell.getCellType();
                        if(type == 0)
                        {
                        value = (long)cell.getNumericCellValue()+"";
                        }
                        else if(type == 1) {
                        value = cell.getStringCellValue();
                        //检查是否已经转换过,已经转换的不再转换
                        if(value.contains("K") || value.contains("M") || value.contains(":"))
                        {
                        continue;
                        }
                        }
                        else {
                        continue;
                        }
                            int lenth = value.length();
                            if(lenth >= 7) {
                            long temp = Long.parseLong(value);
                            //保留两位小数
                            value = String .format("%.2f", (double)temp/1024/1024) + "M";
                            }
                            else if(lenth >= 4)
                            {
                            long temp = Long.parseLong(value);
                            value = String.format("%.2f", (double)temp/1024) + "K";
                            }
                            //System.out.println(value);
                            cell.setCellValue(value);
                            //System.out.println(cell.getStringCellValue());
                        }
           }
            }
            }
            //保存工作薄
            workbook.write(os);
        os.flush();
            os.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
        e.printStackTrace();
        }
        System.out.println("执行完成");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值