你這個程式碼獲取double陣列的時候不需要先做成String的在轉成double的,直接取得double陣列即可,寫入程式碼下面,把double形式的直接寫入
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.ArrayList;import java.util.List;import jxl.Cell;import jxl.NumberCell;import jxl.Sheet;import jxl.Workbook;import jxl.write.Number;import jxl.write.WritableSheet;import jxl.write.WritableWorkbook;public class TestExcelWrite { public double Xnew[]; public double Ynew[]; public static void main(String[] args) throws Exception { Workbook wb = Workbook.getWorkbook(new FileInputStream("D:/test.xls")); Sheet[] sheets = wb.getSheets(); Sheet sheet = sheets[0]; int rows = sheet.getRows(); double X[] = new double[rows]; double Y[] = new double[rows]; List<Double> xList = new ArrayList<>(); List<Double> yList = new ArrayList<>(); for (int i = 0; i < rows; i++) { Cell[] cells = sheet.getRow(i); X[i] = ((NumberCell) cells[0]).getValue(); Y[i] = ((NumberCell) cells[1]).getValue(); } for (int n = 1; n < rows - 2; n++) { if ((Y[n] >= Y[n - 1]) && (Y[n] > Y[n + 1]) || (Y[n] <= Y[n - 1]) && (Y[n] < Y[n + 1]) || (Y[n] > Y[n - 1]) && (Y[n] >= Y[n + 1]) || (Y[n] < Y[n - 1]) && (Y[n] <= Y[n + 1])) { xList.add(X[n]); yList.add(Y[n]); } } Double[] Xnew = xList.toArray(new Double[xList.size()]); Double[] Ynew = yList.toArray(new Double[yList.size()]); String outFileName = "abc.xls"; File outFile = new File(outFileName); if(!outFile.exists()){ outFile.createNewFile(); } FileOutputStream fos = new FileOutputStream(outFile); WritableWorkbook workbook = Workbook.createWorkbook(fos); WritableSheet sheet1 = workbook.createSheet("sheet1", 0); for(int i=0;i<Xnew.length;i++){ sheet1.addCell( new Number(0, i, Xnew[i])); } for(int i=0;i<Ynew.length;i++){ sheet1.addCell( new Number(1, i, Ynew[i])); } workbook.write(); workbook.close(); fos.flush(); fos.close(); }}
你這個程式碼獲取double陣列的時候不需要先做成String的在轉成double的,直接取得double陣列即可,寫入程式碼下面,把double形式的直接寫入
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.ArrayList;import java.util.List;import jxl.Cell;import jxl.NumberCell;import jxl.Sheet;import jxl.Workbook;import jxl.write.Number;import jxl.write.WritableSheet;import jxl.write.WritableWorkbook;public class TestExcelWrite { public double Xnew[]; public double Ynew[]; public static void main(String[] args) throws Exception { Workbook wb = Workbook.getWorkbook(new FileInputStream("D:/test.xls")); Sheet[] sheets = wb.getSheets(); Sheet sheet = sheets[0]; int rows = sheet.getRows(); double X[] = new double[rows]; double Y[] = new double[rows]; List<Double> xList = new ArrayList<>(); List<Double> yList = new ArrayList<>(); for (int i = 0; i < rows; i++) { Cell[] cells = sheet.getRow(i); X[i] = ((NumberCell) cells[0]).getValue(); Y[i] = ((NumberCell) cells[1]).getValue(); } for (int n = 1; n < rows - 2; n++) { if ((Y[n] >= Y[n - 1]) && (Y[n] > Y[n + 1]) || (Y[n] <= Y[n - 1]) && (Y[n] < Y[n + 1]) || (Y[n] > Y[n - 1]) && (Y[n] >= Y[n + 1]) || (Y[n] < Y[n - 1]) && (Y[n] <= Y[n + 1])) { xList.add(X[n]); yList.add(Y[n]); } } Double[] Xnew = xList.toArray(new Double[xList.size()]); Double[] Ynew = yList.toArray(new Double[yList.size()]); String outFileName = "abc.xls"; File outFile = new File(outFileName); if(!outFile.exists()){ outFile.createNewFile(); } FileOutputStream fos = new FileOutputStream(outFile); WritableWorkbook workbook = Workbook.createWorkbook(fos); WritableSheet sheet1 = workbook.createSheet("sheet1", 0); for(int i=0;i<Xnew.length;i++){ sheet1.addCell( new Number(0, i, Xnew[i])); } for(int i=0;i<Ynew.length;i++){ sheet1.addCell( new Number(1, i, Ynew[i])); } workbook.write(); workbook.close(); fos.flush(); fos.close(); }}