package owlGUI;
import java.io.File;
import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class test
{
/*
* 参数名
* @param excelpath 想读取的表格文件地址
* @param newfilepath 想返回的表格文件地址
*/
String excelpath=null;String newfilepath=null;
public static void xlsopen(String excelpath,String nexcelpath) throws IOException
{
Workbook wb1=null;
File file=new File(excelpath);
try
{
wb1=Workbook.getWorkbook(file);
//1.继续遍历列、行数据
Sheet sheet0=wb1.getSheet(0);
int rows=sheet0.getRows();
int cols=sheet0.getColumns();
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
String cellstr=sheet0.getCell(j, i).getContents().toString();
//继续操作Excel表格数据
System.out.println(cellstr);
}
}
}
catch (BiffException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}//打开第一个excel文件
//2.向新excel表中写入数据
//2.1 新表格地址,并新建文件
File finalfile=new File(nexcelpath);
finalfile.createNewFile();//IOException
//2.2 在文件基础之上新建一个book与sheet;
WritableWorkbook wbook=null;
WritableSheet sheet0=wbook.createSheet("合并数据", 0);//创建一个或若干个sheet0
//2.3 建立新label
Label label=new Label(0, 0, nexcelpath, null);//最多4个参数,列、行、数值、(格式)
//2.4 调用sheet0.add方法加入标签即可,有try-catch
try
{
sheet0.addCell(label);
}
catch (RowsExceededException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (WriteException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}