1

我将jexcelapi 用于 android

我写了字符串“Hello jExcelAPI!”,但是当我阅读时,我看到了中文符号。

我该如何解决?

提前致谢。

PS也许有帮助:我不知道 ws.setCharacterSet(cs);

在此处输入图像描述

package ru.elvigl.hello;

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

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class HellojExcelAPIActivity extends Activity {

    final File file = new File("/sdcard/Folder/File.xls");

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void btnExport_Click(View v) throws IOException, RowsExceededException, WriteException {
        WorkbookSettings ws = new WorkbookSettings();

        //ws.setCharacterSet(cs);
        ws.setEncoding("Cp1251");
        ws.setExcelDisplayLanguage("RU");
        ws.setExcelRegionalSettings("RU");
        ws.setLocale(new Locale("ru", "RU"));

        WritableWorkbook workbook = Workbook.createWorkbook(file, ws); 
        WritableSheet sheet = workbook.createSheet("First Sheet", 0); 

        Label label = new Label(0, 0, "Hello jExcelAPI!");
        sheet.addCell(label); 

        workbook.write();
        workbook.close(); 
    }

    public void btnImport_Click(View v) throws BiffException, IOException {

        WorkbookSettings ws = new WorkbookSettings();

        //ws.setCharacterSet(cs);
        ws.setEncoding("Cp1251");
        ws.setExcelDisplayLanguage("RU");
        ws.setExcelRegionalSettings("RU");
        ws.setLocale(new Locale("ru", "RU"));

        Workbook workbook = Workbook.getWorkbook(file, ws); 
        Sheet sheet = workbook.getSheet(0); 
        Cell a1 = sheet.getCell(0,0); 
        String str = a1.getContents();

        TextView tv = (TextView) findViewById(R.id.tvCellValue);
        tv.setText(str);
    }
}
4

2 回答 2

1

这是“WorkbookSettings”类的文档:http: //jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/WorkbookSettings.html看来“setCharacterSet”方法仅用于读取(导入)电子表格,如果有帮助则不导出。至于汉字,我认为它们是由于您的语言环境而显示的。我会使用这些常量来正确实例化您的 Locale 对象,并使用正确的语言和国家构造函数参数:http ://docs.oracle.com/javase/1.4.2/docs/api/java/util/Locale.html

于 2011-12-08T01:00:42.957 回答
0

我不知道该怎么办 :-(

new java.util.Local(Locale.ENGLISH)是不正确的构造函数。

我使用的是jdk-6u29,现在我使用的是jdk-7u1

在 Eclipse 中,我使用:

  • “UTF-8”文本文件编码
  • 编译器编译级别 1.6

我在 Android 和 PC 上创建相同的应用程序,在其中我使用jexcel_android.jar

对于你好这个词:

在电脑上byte[] b = a1.getString().getBytes("UTF-8");

  • [72、101、108、108、111]

在安卓上byte[] b = a1.getString().getBytes("UTF-8");

  • [-28,-96,-128,-26,-108,-128,-26,-80,-128,-26,-80,-128,-26,-68,-128]

如果在十六进制查看器上打开创建的文件

在此处输入图像描述

电脑代码如下:

package ru.elvigl.jexcelwin;

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

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;
import jxl.write.biff.RowsExceededException;

public class MainClass {

    final static File file = new File("D:/File.xls");

    public static void main(String[] args) 
            throws RowsExceededException, WriteException, IOException, BiffException {  

        writeFile();
        readFile();
    }

    private static void writeFile() 
            throws RowsExceededException, WriteException, IOException{

        WritableWorkbook workbook = Workbook.createWorkbook(file); 
        WritableSheet sheet = workbook.createSheet("First Sheet", 0); 

        Label label = new Label(0, 0, "Hello");
        sheet.addCell(label);

        workbook.write();
        workbook.close(); 

        System.out.println("File created");
    }

    private static void readFile() 
            throws BiffException, IOException{

        Workbook workbook = Workbook.getWorkbook(file); 
        Sheet sheet = workbook.getSheet(0); 

        LabelCell a1 = (LabelCell) sheet.getCell(0,0);

        byte[] b = a1.getString().getBytes("UTF-8");

        System.out.println(str);
    }
}

安卓代码如下:

package ru.elvigl.hello;

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

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;
import jxl.write.biff.RowsExceededException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

public class HellojExcelAPIActivity extends Activity {

    final File file = new File("/sdcard/Folder/File.xls");

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void btnExport_Click(View v) throws IOException, RowsExceededException, WriteException {

        WritableWorkbook workbook = Workbook.createWorkbook(file); 
        WritableSheet sheet = workbook.createSheet("First Sheet", 0); 

        Label label = new Label(0, 0, "Hello");
        sheet.addCell(label);

        workbook.write();
        workbook.close(); 
    }

    public void btnImport_Click(View v) throws BiffException, IOException {

        Workbook workbook = Workbook.getWorkbook(file); 
        Sheet sheet = workbook.getSheet(0); 

        LabelCell a1 = (LabelCell) sheet.getCell(0,0);

        byte[] b = a1.getString().getBytes("UTF-8");
    }
}
于 2011-12-08T21:23:54.543 回答