经过UTF-8编码选择Excel文件不产生 [英] excel file not generating after UTF-8 encoding chosen

查看:120
本文介绍了经过UTF-8编码选择Excel文件不产生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code的这一部分是创造xls文件successfuly

 的FileOutputStream FILEOUT =新的FileOutputStream(C:\\\\ Decrypted.xls);
wb.write(FILEOUT);
fileOut.close();

当code的另一部分有这种说法(这是上面的code)之前

  =中新ByteArrayInputStream进行(theCell_00.getBytes());

但是,当我把它改成

  =中新ByteArrayInputStream进行(theCell_00.getBytes(UTF-8));

这部分

 的FileOutputStream FILEOUT =新的FileOutputStream(C:\\\\ Decrypted.xls);
wb.write(FILEOUT);
fileOut.close();

不产生任何XLS文件了。

我需要的编码改为UTF-8作为我行ByteArrayInputStream的已经做了,所以我应该怎么办了code仍然会产生xls文件。

在需要它的情况下,这两个部分是由该函数取。

 公共无效getExcel()抛出异常{    尝试{
        ByteArrayInputStream的在= NULL;
        FileOutputStream中走出= NULL;        HSSFWorkbook WB =新HSSFWorkbook();
        HSSFSheet表= wb.createSheet(新表);        / *
         *的KeyGenerator kgen = KeyGenerator.getInstance(AES); kgen.init(128); SecretKey的关键=
         * kgen.generateKey();字节[] EN codeD = key.getEn codeD();
         *
         * IOUtils.write(EN codeD,新的FileOutputStream(新
         *文件(C:\\\\ \\\\用户\\\\ ABC \\\\桌面key.txt)));
         * /        的FileInputStream鳍=新的FileInputStream(C:\\\\ key.txt);
        DataInputStream以DIN =新DataInputStream所(翅);        BYTE B [] =新的字节[16];        din.read(二);        InputStream的excelResource =新的FileInputStream(路径);
        工作簿RWB = Workbook.getWorkbook(excelResource);
        INT sheetCount = rwb.getNumberOfSheets();
        片RS = rwb.getSheet(0);
        INT行= rs.getRows();
        INT COLS = rs.getColumns();
        的for(int i = 0; I<行;我++){
            对于(INT J = 0; J< Col.length; J ++){
                字符串theCell_00 = rs.getCell(J,I).getContents();
                的System.out.println(单元格内容:+ theCell_00);                在新= ByteArrayInputStream的(theCell_00.getBytes());
                OUT =新的FileOutputStream(C:\\\\ Decrypted.txt);                尝试{                    //的System.out.println(B);
                    SecretKey的键1 =新SecretKeySpec(二,AES);
                    //创建加密器/解密类
                    AESDecrypter加密器=新AESDecrypter(键1);                    encrypter.encrypt(新ByteArrayInputStream进行(theCell_00.getBytes()),
                        新的FileOutputStream(TEMP.TXT));
                    //解密
                    // encrypter.encrypt(,新的FileOutputStream(Encrypted.txt));                    encrypter.decrypt(IN,OUT);                    尝试{
                        如果(满分!= NULL)
                            out.close();
                    } {最后
                        如果(在!= NULL)
                            附寄();
                    }                    // encrypter.decrypt(新
                    // ByteArrayInputStream进行(theCell_00.getBytes(LATIN_1)),新
                    //的FileOutputStream(C:\\\\ Decrypted.txt));
                    字符串文件名=C:\\\\ Decrypted.txt                    的BufferedReader的BufferedReader = NULL;                    尝试{                        //构建的BufferedReader对象
                        的BufferedReader =新的BufferedReader(新的FileReader(文件名));
                        //的System.out.println(bufferedReader.readLine());
                        串线= NULL;                        而((行= bufferedReader.readLine())!= NULL){
                            //处理数据,在这里我们只是把它打印出来                            / *
                             * HSSFWorkbook WB =新HSSFWorkbook(); HSSFSheet表=
                             * wb.createSheet(新表); HSSFRow行= sheet.createRow(2);
                             * /
                            //的System.out.println(ⅰ);                            HSSFRow行= sheet.createRow(I)
                            INT s_col = 0;
                            row.createCell(s_col).setCellValue(线);
                            // s_col ++;
                            // row.createCell(1).setCellValue(新的Date());
                            FileOutputStream中FILEOUT =新的FileOutputStream(C:\\\\ Decrypted.xls);
                            wb.write(FILEOUT);
                            fileOut.close();                            //的System.out.println(线);
                        }                    }赶上(FileNotFoundException异常前){
                        ex.printStackTrace();
                    }赶上(IOException异常前){
                        ex.printStackTrace();
                    } {最后
                        //关闭的BufferedReader
                        尝试{
                            如果(的BufferedReader!= NULL)
                                bufferedReader.close();
                        }赶上(IOException异常前){
                            ex.printStackTrace();
                        }
                    }                }赶上(例外五){
                    e.printStackTrace();
                }            }
        }
        rwb.close();
    }赶上(例外前){
        ex.printStackTrace();
        ex.getMessage();
    }
}


解决方案

被调用AESDecrypter.decrypt预期的数据类型?它是否有采取一个FileOutputStream对象?或者,你可以在一个作家或其他的OutputStream传递?

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

 作家出=新的BufferedWriter(新OutputStreamWriter(新的FileOutputStream(C:\\\\ Decrypted.txt),UTF-8));

This part of my code was creating xls file successfuly

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

when other part of the code had this statement ( which was before the above code )

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

But when I changed it to

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

this part

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

is not generating any xls file anymore.

I need to change the encoding to UTF-8 as I have done in ByteArrayInputStream line, so what should I do that the code still generates xls file.

In case you need it, the two parts are taken from this function.

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();
    }
}

解决方案

What data types are expected by the call to AESDecrypter.decrypt? Does it have to take in a FileOutputStream object? Or can you pass in a Writer or other OutputStream?

I normally do something like this to write UTF-8 output:

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

这篇关于经过UTF-8编码选择Excel文件不产生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆