2 kristenlee1218 kristenlee1218 于 2016.02.22 13:12 提问

急!在线等!谢谢!java将excel特定的单元格写入另一个excel,已经读出来了,不会写入

package com.excel.action;
import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

import jxl.Cell;

import jxl.CellType;
import jxl.LabelCell;

import jxl.Sheet;

import jxl.Workbook;

public class ExcelOperater

{

public static void main(String[] args)

{   
    jxl.Workbook readwb = null;   

    try    

    {   

        //构建Workbook对象, 只读Workbook对象   

        //直接从本地文件创建Workbook   

        InputStream instream = new FileInputStream("F:/tomcat7/webapps/Excel/upload/1.xls");   

        readwb = Workbook.getWorkbook(instream);   

        //Sheet的下标是从0开始   

        //获取第一张Sheet表   

        Sheet readsheet = readwb.getSheet(0);  

        //获得第一行第一列单元的值  

        Cell c00 = readsheet.getCell(1,1);  

        //通用的获取cell值的方式,返回字符串  

        String strc00 = c00.getContents();

        System.out.println("-----------------");

        System.out.println(c00.getContents());

        System.out.println("------------------");

        //获得cell具体类型值的方式  

        if(c00.getType() == CellType.LABEL)  

        {  

            LabelCell labelc00 = (LabelCell)c00;  

            strc00 = labelc00.getString();  

        }  

        //获取Sheet表中所包含的总列数   

        int rsColumns = readsheet.getColumns();   

        //获取Sheet表中所包含的总行数   

        int rsRows = readsheet.getRows();   

        //获取指定单元格的对象引用   

        for (int i = 0; i < rsRows; i++)   

        {   

            for (int j = 0; j < rsColumns; j++)   

            {   

                Cell cell = readsheet.getCell(j, i);   

                System.out.print(cell.getContents() + " ");   

           }   

            System.out.println();   

        }   




        //利用已经创建的Excel工作薄,创建新的可写入的Excel工作薄   

        jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File(   

                "F:/tomcat7/webapps/Excel/upload/11.xls"), readwb);   

        //读取第一张工作表   

        jxl.write.WritableSheet ws = wwb.getSheet(0);   

        //获得第一个单元格对象   

        jxl.write.WritableCell wc = ws.getWritableCell(0, 0);   

        //写入Excel对象   

        wwb.write();   

        wwb.close();   

    } catch (Exception e) {   

        e.printStackTrace();   

    } finally {   

        readwb.close();   

    }   

}

}

2个回答

xionglangs
xionglangs   Rxr 2016.02.22 15:00
已采纳
Royal_lr
Royal_lr   Ds   Rxr 2016.02.22 13:47

创建一个excel文件,,写进去就行了,,

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!