gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:00:36 25 4
gpt4 key购买 nike

我的这部分代码成功创建了 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();
}
}

最佳答案

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

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

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

关于java - 选择 UTF-8 编码后不生成 excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1799977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com