java修改excel表格中的資料
阿新 • • 發佈:2019-02-10
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("執行完成");
}
}