选择UTF-8编码后不生成excel文件

时间:2009-11-25 21:12:10

标签: java excel utf-8 apache-poi

我的代码的这部分是成功创建xls文件

FileOutputStream fileOut = new FileOutputStream("c:\\Decrypted.xls");
wb.write(fileOut);
fileOut.close();

当代码的其他部分有此语句时(在上面的代码之前)

in = new ByteArrayInputStream(theCell_00.getBytes(""));

但是当我把它改成

in = new ByteArrayInputStream(theCell_00.getBytes("UTF-8"));

这部分

FileOutputStream fileOut = new FileOutputStream("c:\\Decrypted.xls");
wb.write(fileOut);
fileOut.close();

不再生成任何xls文件。

我需要将编码更改为UTF-8,就像我在ByteArrayInputStream行中所做的那样,所以我应该怎么做才能生成xls文件。

如果您需要,这两部分取自此功能。

public void getExcel() throws Exception {

    try {
        ByteArrayInputStream in = null;
        FileOutputStream out = null;

        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("new sheet");

        /*
         * KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); SecretKey key =
         * kgen.generateKey(); byte[] encoded = key.getEncoded();
         * 
         * IOUtils.write(encoded, new FileOutputStream(new
         * File("C:\\Users\\abc\\Desktop\\key.txt")));
         */

        FileInputStream fin = new FileInputStream("C:\\key.txt");
        DataInputStream din = new DataInputStream(fin);

        byte b[] = new byte[16];

        din.read(b);

        InputStream excelResource = new FileInputStream(path);
        Workbook rwb = Workbook.getWorkbook(excelResource);
        int sheetCount = rwb.getNumberOfSheets();
        Sheet rs = rwb.getSheet(0);
        int rows = rs.getRows();
        int cols = rs.getColumns();
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < Col.length; j++) {
                String theCell_00 = rs.getCell(j, i).getContents();
                System.out.println("the Cell Content : " + theCell_00);

                in = new ByteArrayInputStream(theCell_00.getBytes(""));
                out = new FileOutputStream("c:\\Decrypted.txt");

                try {

                    // System.out.println(b);
                    SecretKey key1 = new SecretKeySpec(b, "AES");
                    // Create encrypter/decrypter class
                    AESDecrypter encrypter = new AESDecrypter(key1);

                    encrypter.encrypt(new ByteArrayInputStream(theCell_00.getBytes()),
                        new FileOutputStream("temp.txt"));
                    // Decrypt
                    // encrypter.encrypt(,new FileOutputStream("Encrypted.txt"));

                    encrypter.decrypt(in, out);

                    try {
                        if (out != null)
                            out.close();
                    } finally {
                        if (in != null)
                            in.close();
                    }

                    // encrypter.decrypt(new
                    // ByteArrayInputStream(theCell_00.getBytes(Latin_1)),new
                    // FileOutputStream("c:\\Decrypted.txt"));
                    String filename = "c:\\Decrypted.txt";

                    BufferedReader bufferedReader = null;

                    try {

                        // Construct the BufferedReader object
                        bufferedReader = new BufferedReader(new FileReader(filename));
                        // System.out.println(bufferedReader.readLine());
                        String line = null;

                        while ((line = bufferedReader.readLine()) != null) {
                            // Process the data, here we just print it out

                            /*
                             * HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet =
                             * wb.createSheet("new sheet"); HSSFRow row = sheet.createRow(2);
                             */
                            // System.out.println(i);

                            HSSFRow row = sheet.createRow(i);
                            int s_col = 0;
                            row.createCell(s_col).setCellValue(line);
                            // s_col++;
                            // row.createCell(1).setCellValue(new Date());
                            FileOutputStream fileOut = new FileOutputStream("c:\\Decrypted.xls");
                            wb.write(fileOut);
                            fileOut.close();

                            // System.out.println(line);
                        }

                    } catch (FileNotFoundException ex) {
                        ex.printStackTrace();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    } finally {
                        // Close the BufferedReader
                        try {
                            if (bufferedReader != null)
                                bufferedReader.close();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }

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

            }
        }
        rwb.close();
    } catch (Exception ex) {
        ex.printStackTrace();
        ex.getMessage();
    }
}

1 个答案:

答案 0 :(得分:0)

调用AESDecrypter.decrypt需要哪些数据类型?是否必须接受FileOutputStream对象?或者你可以传入Writer或其他OutputStream吗?

我通常会做这样的事情来编写UTF-8输出:

Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("c:\\Decrypted.txt"), "UTF-8"));