无法使用jexceapi对excel表进行编码

时间:2011-10-04 07:21:30

标签: java jexcelapi

我必须使用JExcel api创建一个excel表。我无法对excel文件进​​行编码,并且无法确定文件是否已编码?... 请帮忙

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;

import jxl.Sheet;
import jxl.SheetSettings;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.read.biff.BiffException;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class Jexcel_test {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    try
    {       

        WorkbookSettings ws = new WorkbookSettings();
        ws.setLocale(new Locale("en", "EN"));        
        ws.setCharacterSet(0);
        ws.setEncoding("utf-8");
        ws.setDrawingsDisabled(true);
        ws.setGCDisabled(true);

        System.out.println(ws.getCharacterSet());
        System.out.println(ws.getArrayGrowSize());
        System.out.println(ws.getExcelDisplayLanguage());
        System.out.println(ws.getExcelRegionalSettings());


        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter filename  :");
        File filename= new File("f:/"+br.readLine()+".xls"); 

        Workbook wb1=Workbook.getWorkbook(filename, ws);

        Sheet s  = wb1.getSheet(0);

       SheetSettings settings = s.getSettings();
        settings.setDefaultRowHeight(500);
        if(!settings.isProtected())
        {               
            settings.setPassword("test");               
        }   
        settings.setFitToPages(true);   

        System.out.println(s.getName());


        for (int row = 1; row < s.getRows(); row++) {
            String val = s.getCell(0, row).getContents().trim();
            System.out.println(val);
        }


        wb1.close();


        System.out.println("Enter new filename  :");
        File filename1= new File("f:/"+br.readLine()+".xls"); 
        WritableWorkbook wb2 = Workbook.createWorkbook(filename1, ws);
        WritableSheet sheet = wb2.createSheet("mysheet",0); 

        SheetSettings settings1 = sheet.getSettings();
        settings1.setDefaultRowHeight(500);
        settings1.setProtected(true);
        settings1.setPassword("test");
        settings1.setFitToPages(true);  
        System.out.println("Workbook  "+filename1+"  created");

        wb2.write();
        wb2.close();

    }
    catch (IOException e)
    {
      e.printStackTrace();
    }

    catch (BiffException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (WriteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

1 个答案:

答案 0 :(得分:0)

首先在其中一个列中写一些非ascii数据,然后读取相同的内容..你会得到答案。