java代码
package exc;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class SgaqglddUtil {
/**
*
* 函数功能说明 :读取excel
* @param path
* @throws
*/
public static void read(String path){
try {
Workbook book = Workbook.getWorkbook( new File( path ));
// 获得第一个工作表对象
Sheet sheet = book.getSheet( 0 );
// 得到第一列第一行的单元格
Cell cell1 = sheet.getCell( 0 , 0 );
String result = cell1.getContents();
System.out.println(result);
book.close();
} catch (Exception e) {
System.out.println(e);
}
}
public static void write(String path){
try {
// Excel获得文件
Workbook wb = Workbook.getWorkbook( new File(path));
// 打开一个文件的副本,并且指定数据写回到原文件
WritableWorkbook book = Workbook.createWorkbook( new File(path),
wb);
WritableSheet sheet1=book.getSheet(0); // 获取第一个工作表
// WritableSheet sheet = book.createSheet( " 第二页 " , 1 );//创建一个工作表
int y=0,x=1;
sheet1.addCell( new Label( y , x, "测试数据 "));//在第向x+1行,y+1列添加“测试数据”
book.write();
book.close();
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String args[]) throws IOException {
String path="E:\\bb.xls";
SgaqglddUtil.copyFile(path, "E:\\excel\\b2.xls");
SgaqglddUtil.write(path);
SgaqglddUtil.read(path);
}
// 复制文件
public static void copyFile(String sourceFile, String targetFile) throws IOException {
// File file=new File(targetFile);
// file.mkdirs();
BufferedInputStream inBuff = null;
BufferedOutputStream outBuff = null;
try {
// 新建文件输入流并对它进行缓冲
inBuff = new BufferedInputStream(new FileInputStream(sourceFile));
// 新建文件输出流并对它进行缓冲
outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));
// 缓冲数组
byte[] b = new byte[1024 * 5];
int len;
while ((len = inBuff.read(b)) != -1) {
outBuff.write(b, 0, len);
}
// 刷新此缓冲的输出流
outBuff.flush();
} finally {
// 关闭流
if (inBuff != null)
inBuff.close();
if (outBuff != null)
outBuff.close();
}
}
}