版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yuming226/article/details/84109318
Excel读取数据
package filePackage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class Demo4_FileInputStream {
public static void main(String[] args) throws BiffException, IOException {
Workbook workbook=Workbook.getWorkbook(new File("E:/Project/test/j.xls"));
BufferedWriter writer = new BufferedWriter(new FileWriter(new File("c:\\d.doc")));
Sheet sheet=workbook.getSheet(2);
int rows = sheet.getRows();
int columns = sheet.getColumns();
System.out.println(rows + "\t" + columns);
for(int i=0;i<sheet.getRows();i++){
for(int j=0;j<sheet.getColumns()-10;j++){
Cell cell=sheet.getCell(j,i);
writer.write(cell.getContents()+"\t");
}
writer.write("\n");
}
workbook.close();
writer.close();
}
}
excel中添加数据
从txt文件和Excel中读取数据,再将数据添加到Excel中。
package filePackage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Reader;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class Demo6_FileInputStream {
public static void main(String[] args) throws Exception {
String path = "C:\\Users\\Administrator\\Desktop\\Test\\jlFile.txt";
File f = new File(path);
BufferedReader fis = new BufferedReader(new FileReader(f));
Workbook workbook=Workbook.getWorkbook(new File("E:/project/test/j.xls"));
File file = new File("E:/project//test/j.xls");
WritableWorkbook wb = Workbook.createWorkbook(file);
WritableSheet sheet = wb.getSheet(0);
WritableSheet sheet1 = wb.getSheet(1);
String str = null;
String[] str1 = new String[1024];
int num = 0;
Label lable1 = null;
Label lable2 = null;
Label lable3 = null;
Label lable4 = null;
int n =0;
Sheet sheet3=workbook.getSheet(0);
for (int k =0; k < 16; k++) {
int i = 1;
Cell cell=sheet3.getCell(k,2);
String strc= cell.getContents();
while ((str=fis.readLine()) !=null) {
if(k == 1 || k == 2 || k == 5) {
str1[n++] = str;
lable1 = new Label(1,i,str);
lable2 = new Label(2,i,str);
lable3 = new Label(5,i,str);
sheet.addCell(lable1);
sheet.addCell(lable2);
sheet.addCell(lable3);
} else {
lable4 = new Label(k,i,strc);
sheet.addCell(lable4);
}
i++;
}
}
Sheet sheet4=workbook.getSheet(1);
for (int k =0; k < 7; k++) {
Cell cell=sheet4.getCell(k,2);
String strc= cell.getContents();
for (int j = 0; j < n; j++) {
if(k == 1){
lable4 = new Label(1,j+1,str1[j]);
sheet1.addCell(lable4);
} else {
lable4 = new Label(k,j+1,str1[j]);
sheet1.addCell(lable4);
}
}
}
wb.write();
workbook.close();
wb.close();
fis.close();
}
}