简体   繁体   中英

Read and write to the same Excel file

package jexcel.jxl.nimit;  

import java.io.File;  
import java.io.IOException;  

import jxl.Cell;  
import jxl.CellType;  
import jxl.LabelCell;  
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;  


public class ExcelJxl {

    public static void main(String[] args) throws WriteException, IOException,     BiffException{
        String S="D:\\nimit.xls";
        ExcelJxl.WriteFile(S);
    }
    public static void WriteFile(String path) throws IOException, WriteException, BiffException{
            Workbook wb=Workbook.getWorkbook(new File(path));
           Sheet sheet=wb.getSheet(0);
            String s1=null;
            String s2=null;
            Cell c1=sheet.getCell(0,0);
            Cell c2=sheet.getCell(1,0);
             if (c1.getType() == CellType.LABEL)
            {
              LabelCell lc = (LabelCell) c1;
               s1 = lc.getString();
            } 
            if (c2.getType() == CellType.LABEL)
            {
          LabelCell lb = (LabelCell) c2;
           s2 = lb.getString();
        }
        String s3=s1+s2;

    WritableWorkbook copy=Workbook.createWorkbook(new File("D:\\demo.xls"),wb);
    WritableSheet Wsheet = copy.getSheet(0); 
    Label l1=new Label(0,0,s1);
    Wsheet.addCell(l1);
    Label l2=new Label(1,0,s2);
    Wsheet.addCell(l2);
    Label l3 = new Label(2, 0,s3);
    Wsheet.addCell(l3);
    copy.write();
    copy.close();
 }
}

I'm trying to learn how to read and write to the same Excel file.

I'm taking two strings from one file and putting in the other.

How to put the content into the same file?

I'm creating a new file and then I'm putting the contents. How to read and write to the same text file? How to overcome this problem?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM