永发信息网

jxl读取excel,知道密码,怎么读取excel-CSDN论坛

答案:1  悬赏:10  手机版
解决时间 2021-01-27 21:46
  • 提问者网友:杀生予夺
  • 2021-01-26 21:13
jxl读取excel,知道密码,怎么读取excel-CSDN论坛
最佳答案
  • 五星知识达人网友:忘川信使
  • 2021-01-26 21:29
用jxl读取excel的数据,由于excel数据在录入时的各种原因,数据后面都有空格,而且读出来以后(也许是编码原因),数据口面不是出

现"?"就是出现一个不知所谓的乱码符,不要考虑用替换,因为替换只有在你的项目编码方式和内存中excel数据编码方式一样的时候才能替换,否则你连保

存都会提示编码问题而保存不了。直接用
subSequence(0, cellContent.length()-1)
就可以了
同时提醒一下读取出来的数据时Cell类型的话,直接getContent是可以得到内容的,但具体内容最好依靠下面的方法获

Java代码

if (cell.getType() == CellType.LABEL) {
LabelCell labelCell = (LabelCell) cell;
String cellContent = labelCell.getString();
cellContent = (String) cellContent.subSequence(0, cellContent.length()-1);
column_contents[cols] = cellContent;
}
else if (cell.getType() == CellType.NUMBER) {
//number的话不用去空格就可以,我测试是这样
NumberCell numberCell = (NumberCell) cell;
String cellContent = numberCell.getContents();
column_contents[cols] = cellContent;
}
else if (cell.getType() == CellType.DATE) {
DateCell dateCell = (DateCell) cell;
Date dateDemo = dateCell.getDate();
String cellContent = dateDemo.toString();
column_contents[cols] = cellContent;
}
我之前和朋友交流时朋友给我一段代码,你也可以试一下
1 import java.io.File;
2 import java.io.FileInputStream;
3
4 import jxl.Cell;
5 import jxl.Sheet;
6 import jxl.Workbook;
7
8
9 public class testxls {
10
15 public static String xls2String(File file){
16 String result = "";
17 try{
18 FileInputStream fis = new FileInputStream(file);
19 StringBuilder sb = new StringBuilder();
20 jxl.Workbook rwb = Workbook.getWorkbook(fis);
21 Sheet[] sheet = rwb.getSheets();
22 for (int i = 0; i < sheet.length; i++) {
23 Sheet rs = rwb.getSheet(i);
24 for (int j = 0; j < rs.getRows(); j++) {
25 Cell[] cells = rs.getRow(j);
26 for(int k=0;k 27 sb.append(cells[k].getContents());
28 }
29 }
30 fis.close();
31 result += sb.toString();
32 }catch(Exception e){
33 e.printStackTrace();
34 }
35 return result;
36 }
37 public static void main(String[] args){
38 File file = new File("D:/luceneData/test5.xls");
39 System.out.println(xls2String(file));
40 }
41 }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯