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("执行完成");
}
}
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("执行完成");
}
}