Java 导出数组到EXcel
我希望将代码中的Xnew[]Ynew[]导出到到Excel表中第一列存Xnew数组第二列存Ynew数组[]麻烦各位大神给个代码参考谢谢真的很感激importjava.io...
我希望将代码中的Xnew[] Ynew[]导出到到Excel表中 第一列存Xnew数组 第二列存Ynew数组[] 麻烦各位大神给个代码参考 谢谢 真的很感激
import java.io.*;
import jxl.*;
import jxl.write.*;
import jxl.write.biff.LabelRecord;
import jxl.write.biff.RowsExceededException;
import jxl.format.*;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.awt.Color;
import jxl.write.Number;
import jxl.write.Boolean;
public class tese {
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];
String[] x = new String[sheet.getRows()];
String[] y = new String[sheet.getRows()];
double X[] = new double [sheet.getRows()];
double Y[] = new double [sheet.getRows()];
List<String> xList = new ArrayList<>();
List<String> yList = new ArrayList<>();
for (int i = 0; i < sheet.getRows(); i++) {
Cell[] cells = sheet.getRow(i);
x[i] = String.valueOf(((NumberCell)cells[0]).getValue());
y[i] = String.valueOf(((NumberCell)cells[1]).getValue());
}
for(int j =0; j <sheet.getRows();j++)
{
X[j]= Double.parseDouble(x[j]);
Y[j]= Double.parseDouble(y[j]);
}
for(int n=1 ;n<sheet.getRows()-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]);
}
}
String[] Xnew = xList.toArray(new String[xList.size()]);
String[] Ynew = yList.toArray(new String[yList.size()]);
System.out.println(Xnew.length);
}
} 展开
import java.io.*;
import jxl.*;
import jxl.write.*;
import jxl.write.biff.LabelRecord;
import jxl.write.biff.RowsExceededException;
import jxl.format.*;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.awt.Color;
import jxl.write.Number;
import jxl.write.Boolean;
public class tese {
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];
String[] x = new String[sheet.getRows()];
String[] y = new String[sheet.getRows()];
double X[] = new double [sheet.getRows()];
double Y[] = new double [sheet.getRows()];
List<String> xList = new ArrayList<>();
List<String> yList = new ArrayList<>();
for (int i = 0; i < sheet.getRows(); i++) {
Cell[] cells = sheet.getRow(i);
x[i] = String.valueOf(((NumberCell)cells[0]).getValue());
y[i] = String.valueOf(((NumberCell)cells[1]).getValue());
}
for(int j =0; j <sheet.getRows();j++)
{
X[j]= Double.parseDouble(x[j]);
Y[j]= Double.parseDouble(y[j]);
}
for(int n=1 ;n<sheet.getRows()-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]);
}
}
String[] Xnew = xList.toArray(new String[xList.size()]);
String[] Ynew = yList.toArray(new String[yList.size()]);
System.out.println(Xnew.length);
}
} 展开
展开全部
你这个代码获取double数组的时候不需要先做成String的在转成double的,直接取得double数组即可,写入代码下面,把double形式的直接写入
import 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();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |