java 对Excel的读取和写入

来源:互联网 发布:刺客信条大革命优化差 编辑:程序博客网 时间:2024/06/05 14:21

详情可联系本人,欢迎探讨:QQ:513589695

首先导入API,分别是:

jxl.jar、javax.servlet.jar、poi...等三个包,如图,直接百度就可以下载。

导入方法:在所建工程JRE System Library 右击-->build path-->Config....--->Libraries--->add External JAR 直接把压缩包添加就好


然后就是直接编程了:



import java.io.*;


import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import java.io.*;
import jxl.*;
import jxl.write.*;


import org.apache.poi.hslf.model.Sheet;
import org.apache.poi.hssf.record.formula.functions.Cell;
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;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;


public class doExcel{


/**
* @param args
*/
static String name1;
static String name2;
static String class1;
static String class2;
static String ID;

public static void main(String[] args) {
// TODO Auto-generated method
String[] name=new String[15334];
String[] ClassT=new String[15334];
String[] IDT=new String[15334];
for(int i=0;i<=15333;i++)
{ int m=15333-i;
name[i]=getname2(m);
ClassT[i]=getClass2(m);
IDT[i]=getID(m);
System.out.println(i);
}
for(int row1=1246;row1<=4439;row1++){
name1=getname1(row1);
class1=getClass1(row1).substring(4,7);
System.out.print(class1);
for(int row2=15334;row2>=1;row2--){
int t=15334-row2;
if(name1.equals(name[t])&&class1.equals(ClassT[t].substring(0,3))){
System.out.println("\n"+row1+"个:"+name1);
writeToClass(row1, ClassT[t]);
writeToID(row1,IDT[t]);
break;
}
else 
System.out.println("  "+row2);
}
}
System.out.print("end!!!");


}

//从文档1中获取名字
public static String getname1(int row)
{String result = null;
try 

Workbook book= 
Workbook.getWorkbook(new File("F:/1.xls")); 
//获得第一个工作表对象 
jxl.Sheet sheet=book.getSheet(0); 
//得到第一列第一行的单元格 
jxl.Cell cell1=sheet.getCell(1,row); 
result=cell1.getContents(); 
book.close(); 
}catch(Exception e) 

System.out.println(e); 
}
return result; 


}


//从文档2中获取名字
public static String getname2(int row)
{
String result = null;
try 

Workbook book= 
Workbook.getWorkbook(new File("F:/2.xls")); 
//获得第一个工作表对象 
jxl.Sheet sheet=book.getSheet(0); 
//得到第一列第一行的单元格 
jxl.Cell cell1=sheet.getCell(1,row); 
result=cell1.getContents(); 
book.close(); 
}catch(Exception e) 

System.out.println(e); 
}
return result; 
}

//获取学生ID
public static String getID(int row)
{
String result = null;
try 

Workbook book= 
Workbook.getWorkbook(new File("F:/2.xls")); 
//获得第一个工作表对象 
jxl.Sheet sheet=book.getSheet(0); 
//得到第一列第一行的单元格 
jxl.Cell cell1=sheet.getCell(0,row); 
result=cell1.getContents(); 
book.close(); 
}catch(Exception e) 

System.out.println(e); 
}
return result; 
}

//获取班级信息
public static String getClass2(int row)
{
String result = null;
try 

Workbook book= 
Workbook.getWorkbook(new File("F:/2.xls")); 
//获得第一个工作表对象 
jxl.Sheet sheet=book.getSheet(0); 
//得到第一列第一行的单元格 
jxl.Cell cell1=sheet.getCell(3,row); 
result=cell1.getContents(); 
book.close(); 
}catch(Exception e) 

System.out.println(e); 
}
return result; 
}
public static String getClass1(int row)
{
String result = null;
try 

Workbook book= 
Workbook.getWorkbook(new File("F:/1.xls")); 
//获得第一个工作表对象 
jxl.Sheet sheet=book.getSheet(0); 
//得到第一列第一行的单元格 
jxl.Cell cell1=sheet.getCell(8,row); 
result=cell1.getContents(); 
book.close(); 
}catch(Exception e) 

System.out.println(e); 
}
return result; 
}


//写入学号信息

@SuppressWarnings("deprecation")
public static void writeToID(int row1,String ID1){
//写入数据

try{
POIFSFileSystem fs =new POIFSFileSystem(new FileInputStream("F:/1.xls"));
   HSSFWorkbook wb = new HSSFWorkbook(fs);
   HSSFSheet sheet = wb.getSheetAt(0);
   HSSFRow row = sheet.getRow(row1);//第几行
   HSSFCell cell = row.getCell((short)5);//第几列
   cell.setCellValue(ID1);
   // Write the output to a file
   FileOutputStream fileOut = new FileOutputStream("F:/1.xls");
   wb.write(fileOut);
fileOut.close();
}
catch(Exception e){
  e.printStackTrace();
}
}

//写入班级
@SuppressWarnings("deprecation")
public static void writeToClass(int row1,String Class){
//写入数据
try{
POIFSFileSystem fs =new POIFSFileSystem(new FileInputStream("F:/1.xls"));
   HSSFWorkbook wb = new HSSFWorkbook(fs);
   HSSFSheet sheet = wb.getSheetAt(0);
   HSSFRow row = sheet.getRow(row1);
   HSSFCell cell = row.getCell((short)9);
   cell.setCellValue(Class);
   // Write the output to a file
   FileOutputStream fileOut = new FileOutputStream("F:/1.xls");
   wb.write(fileOut);
fileOut.close();
}
catch(Exception e){
  e.printStackTrace();
  }
}
}





原创粉丝点击