java中怎样在excel中写java 写入数据到excel

android(108)
java向excel读写数据的步骤:
首先,需要jxl.jar包。
读excel文件:
public class Test2 {
& & public static void main(String[] args) {
& & & & try {
& & & & & & Workbook book = Workbook.getWorkbook(new File(&测试.xls&));
& & & & & & // 获得第一个工作表对象
& & & & & & Sheet sheet = book.getSheet(0);
& & & & & & // 得到单元格
& & & & & & for (int i = 0; i & sheet.getColumns(); i++) {
& & & & & & & & for (int j = 0; j & sheet.getRows(); j++) {
& & & & & & & & & & Cell cell = sheet.getCell(i, j);
& & & & & & & & & & System.out.print(cell.getContents() + & &&);
& & & & & & & & }
& & & & & & & & System.out.println();
& & & & & & }
& & & & & & book.close();
& & & & } catch (Exception e) {
& & & & & & System.out.println(e);
向excel中写数据:
&* jxl 创xcel
public void writerJXL() {
& & & & long dir= (long) Math.round(Math.floor(Math.random() * ));
& & & & String dirPath =&c://11//&.concat(String.valueOf(dir)) ;
& & & & File file = new File(dirPath);
& & & & if(file.exists()) {
& & & & & & System.out.println(file.getName()+&存xl&);
& & & & & & if(file.delete()) {
& & & & & & & & file.mkdir();
& & & & & & }
& & & & }else {
& & & & & & file.mkdir();
& & & & // & & &打 &&
& & & & long l = (long) Math.round(Math.floor(Math.random() * ));
& & & & String name = &jxl_&.concat(String.valueOf(l)).concat(&.xls&);
& & & & WritableWorkbook book = Workbook.createWorkbook( new &File(dirPath.concat(&//&).concat(name)));
& & & & int j = 0 ;
& & & & while(j & sheet){ //控heet创
& & & & & & WritableSheet sheet &= &book.createSheet(&test&+j ,j);
& & & & & & Label label1 =
& & & & & & Label label2 =
& & & & & & for(int i = 0 ; i &i ++){
& & & & & & & & label1 &= & new &Label(0,i,&A数xl&+i ); //第第
& & & & & & & & label2 &= & new &Label(1,i,&A数xl&+i ); & & & & & &
& & & & & & & & sheet.addCell(label1); & &// &将 & &
& & & & & & & & sheet.addCell(label2); & & & & & & &&
& & & & & & }
& & & & & & j++ ;
& & & & } & & & &
& & & & book.write();
& & & & book.close();
& & } catch (IOException e) {
& & & & // TODO Auto-generated catch block
& & & & e.printStackTrace();
& & } catch (RowsExceededException e) {
& & & & // TODO Auto-generated catch block
& & & & e.printStackTrace();
& & } catch (WriteException e) {
& & & & // TODO Auto-generated catch block
& & & & e.printStackTrace();
&&相关文章推荐
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:227982次
积分:5076
积分:5076
排名:第5003名
原创:273篇
转载:133篇
评论:21条
文章:21篇
阅读:17660
文章:20篇
阅读:11925
文章:18篇
阅读:10366
文章:26篇
阅读:19177
(2)(1)(7)(12)(17)(6)(1)(6)(2)(23)(7)(7)(55)(47)(110)(40)(63)本帖子已过去太久远了,不再提供回复功能。& & 可随意转载,但请注明出处及作者 SonyMusic
========================================================================== 在 Java 中向Excel文件写入内容 四、导出数据到Excel文件中 下面的例子,设置了数字、日期的格式,还有字体,颜……
声明:该文章系网友上传分享,此内容仅代表网友个人经验或观点,不代表本网站立场和观点;若未进行原创声明,则表明该文章系转载自互联网;若该文章内容涉嫌侵权,请及时向
论文写作技巧
上一篇:下一篇:
相关经验教程由于有两个测试环境,有时候涉及到从这个测试环境把配置数据移到另外个环境。可以从直接操作,但是为了练+selenium+jxl,所以我从web页面上进行操作。思路如下:
1.从这个测试环境中获取配置,并写入表格;
2.进入到另外一个测试环境,通过页面的新增功能,逐条读取Excel表格中的数据,并新增。
相关代码段如下:
写Excel文件的代码:
* //向Excel表格写入页面table中的数据
* @param className
* @param driver
* @param table
* @param refreshElement
public static void outPutFromTable(String className,WebDriver driver,WebElement table,String refreshElement){
//获取类名为工作薄名
String excelName = getClassToExcelName(className);
//获取方法名为工作表名
String sheetName = Common.getRunFromCallFunc();
//先获取表头列名
List titleNames = ElementOperater.getTableTitleName(driver);
//获取表格内容
List&String& contentName = ElementOperater.getPageTableContent(driver, table, refreshElement);
WritableWorkbook wk = Workbook.createWorkbook(new File(excelName+".xls"));//创建一个工作薄
WritableSheet st = wk.createSheet(sheetName, 0);//创建一个工作表
Label lab =
for(int r=0;r&contentName.size();r++){//根据表格中的内容循环
for(int c=0;c&titleNames.size();c++){//根据表头的列数循环
if(r==0&&c&titleNames.size()){//如果是第一行,并且列数小于列的长度
lab = new Label(c,r,(String)titleNames.get(c));//创建第c列第r行的Label对象
st.addCell(lab);//把label对象添加入表sheet的单元格
} catch (WriteException e) {
e.printStackTrace();
lab = new Label(c,r,(String)contentName.get(c));
st.addCell(lab);
} catch (WriteException e) {
e.printStackTrace();
wk.write();//写入
wk.close();//关闭
} catch (WriteException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
读取Excel的代码参考另一篇博文
https://my.oschina.net/bysu/blog/832256}

我要回帖

更多关于 java把数据写入excel 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信