jxl读取excel时中文全角空格问题

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/daughter_wall/article/details/84006888

用jxl读取excel的数据,由于excel数据在录入时的各种原因,数据后面都有空格,而且读出来以后(也许是编码原因),数据口面不是出现"?"就是出现一个不知所谓的乱码符,不要考虑用替换,因为替换只有在你的项目编码方式和内存中excel数据编码方式一样的时候才能替换,否则你连保存都会提示编码问题而保存不了。

直接用subSequence(0, cellContent.length()-1) 就可以了

同时提醒一下,读取出来的数据时Cell类型的话,直接getContent是可以得到内容的,但具体内容最好依靠下面的方法获

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;
}
展开阅读全文

jxl读取excel

11-26

[code=Java]public static void main(String[] args) rn try rn Workbook workbook = null;rn try rn workbook = Workbook.getWorkbook(new File(rn "F:\\(20100268号存量)测试案例.xls"));rn catch (Exception e) rn throw new Exception("file to import not found!");rn rn int sheetSize = workbook.getNumberOfSheets();rn Sheet sheet = null;rnrn for (int i = 0; i < sheetSize; i++) rn sheet = workbook.getSheet(i);rn rn Cell cell = null;rnrn List list = new ArrayList();rn int rowCount = sheet.getRows();rn Class proj = project.class;rn // 根据class对象获取属性rn Field[] fields = proj.getDeclaredFields();rn project proj1[] = new project[rowCount];rn for (int i = 2; i < sheet.getRows(); i++) rn proj1[i] = new project();rn for (int j = 0; j < sheet.getColumns(); j++) rn cell = sheet.getCell(j, i);// 获取单元格对象rn rn String propertity = fields[j].getName().substring(0,rn fields[j].getName().length()); rn rn rn proj1[i] = getProject(propertity, cell.getContents(),rn proj1[i]); //java反射造对象rn rn rn list.add(proj1[i]);rn rn rn workbook.close();rn System.out.println(list.size());rn for (int i = 0; i < list.size(); i++) rn project p = (project) list.get(i);rn System.out.println("pid=" + p.getPid());rn rn catch (Exception e) rn e.printStackTrace();rn rn rn[/code]rnrn打印结果:rnrn[code=Java]15rnpid=日终网点签退统计-001rnpid=rnpid=rnpid=日终网点签退统计-002rnpid=rnpid=rnpid=日终网点签退统计-003rnpid=rnpid=rnpid=日终网点签退统计-004rnpid=rnpid=rnpid=日终网点签退统计-005rnpid=rnpid=[/code]rnrnrn现在有合并单元格的垃圾数据.我现在想每次都拿一行,赋给对象,然后判断.pid是否为空.在做操作,现在怎么拿到行进行操作呢? 论坛

全角空格和半角空格??????

09-22

我从数据库中抽出的字段中可能有全角的字符也有半角的字符,要求如果此字段没有和其定义的字段的长度相等,就在其字段的后补足全角空格,rnrn例如rnname varchar(50)rnrn1,name="a",rnrn就应该补足24个全角的空格和一个半角的空格rnrn2,name="ab",rnrn就应该补足24个全角的空格rnrn请各位大哥帮忙解决以下,我写了一个函数,但不能解决1,name="a"的情况,我是菜鸟,能否提供更好的解决办法,谢谢!rn(1)char * str_Io_P /* 为要转换的字符串 */rn(2)int li_Dem_Len /* 要求的长度,就是字段的长度 */rn(3)int li_SelFlag /* 是全角转换还是半角转换 */rnrnvoid Add_Space_ ( char * str_Io_P, int li_Dem_Len, int li_SelFlag )rnrn if ( (li_SelFlag == BATCH_SEL_1 || li_SelFlag == BATCH_SEL_2)rnrn && str_Io_P != NULL rn && ( li_Dem_Len >= 0 || li_Dem_Len <= MAX_BUFFER_LEN ))rn rn /* •Ï”’è‹` */rn int li_Count = 0 ;rn int li_True_Len = 0 ;rn char lc_str[MAX_BUFFER_LEN] ;rn char * w_Temp_P = NULL;rnrn /* €–Ú’l‰Šú‰»‚Ɛݒ菈— */rn memset( lc_str ,0x00, MAX_BUFFER_LEN ) ;rn w_Temp_P = str_Io_P + li_Dem_Len - 1 ;rn while ( *w_Temp_P == 0x20 )rn rn w_Temp_P--;rn rn strncpy( lc_str, str_Io_P, w_Temp_P- str_Io_P + 1) ;rn li_True_Len = strlen( lc_str );rn /* Œ‹‰ÊÝ’菈— */rn if ( li_True_Len != li_Dem_Len )rn rn /* FULL SPACE‚ðÝ’è‚·‚éB */rn if ( li_SelFlag == BATCH_SEL_1 )rn rn li_True_Len = (int)ceil((double)( li_Dem_Len - li_True_Len ) / 2);rnrn for ( li_Count = 0;li_Count < li_True_Len ; li_Count++ )rn rn strcat( lc_str, "@" ) ;rn rn rn /* Half SPACE‚ðÝ’è‚·‚éB */rn else if ( li_SelFlag == BATCH_SEL_2 ) rn rn li_True_Len = li_Dem_Len - li_True_Len ;rn for ( li_Count = 0; li_Count < li_True_Len ; li_Count++ )rn rn strcat( lc_str, " " ) ;rn rn rn sprintf( str_Io_P, "%s", lc_str ) ;rn rn rnrnrnrnrnrnrn 论坛

jxl读取Excel奇怪报错

11-19

[code=Java]rn/**rn * rn */rnpackage com.vteam.mul;rnrnimport java.io.File;rnimport java.util.HashMap;rnimport java.util.Iterator;rnimport java.util.List;rnimport java.util.Set;rnrnimport jxl.Cell;rnimport jxl.CellType;rnimport jxl.LabelCell;rnimport jxl.Sheet;rnimport jxl.Workbook;rnrn/**rn * @作者 Jarvisrn * @创建日期 Nov 19, 2009rn * @版本 V 1.0rn */rnrnpublic class ReadExcel rnrn /**rn * @param argsrn */rn private String ExcelPath ;rn private HashMap ExcelMap ;rn private int column1 ;//指定读取列1 从0 开始rn private int column2 ;//指定读取列2 从0 开始rn private int rowStart;//读取起始行下标 从0 开始rn rn public ReadExcel()rn rn rn public ReadExcel(String ExcelPath)rn this.ExcelPath = ExcelPath ;rn rn/**rn * 读取Excel 按照sheet名称顺序读取rn * 需要设置Excel路径ExcelPath,读取起始行rowStart,读取的2列column1和column2rn * @returnrn */rn public HashMap ReadExcel()rn tryrn rn ExcelMap = new HashMap();//存储 中文key-英文value rn File file = new File(this.ExcelPath);rn Workbook rwb = Workbook.getWorkbook(file);rn String[] sheets = rwb.getSheetNames();rn// rwb.close();rn// Sheet st = rwb.getSheet(this.getSheetNo()) ;//下标获取表名,从0开始rn for(int i = 0 ; i< sheets.length ; i++) rn// Workbook temrwb = Workbook.getWorkbook(file);rn Sheet st = rwb.getSheet(sheets[i]);//sheet名称获取方式 rn int row = st.getRows(); // /得到该sheet的行数rn// int column = st.getColumns(); // 得到该sheet的列数rn int starrow = this.rowStart ;rn Cell key = null ;rn Cell value = null ;rn while(starrow < 10) rn key = st.getCell(this.getColumn1(),starrow);//第一个参数代表列 第二个参数代表行rn value = st.getCell(this.getColumn2(),starrow);rn //通用的获取cell值的方式,返回字符串rn String keyStr = key.getContents();rn String valueStr = key.getContents();rn //获得cell具体类型值的方式rn if(key.getType() == CellType.LABEL)rn rn LabelCell labelkey = (LabelCell)key;rn keyStr = labelkey.getString();rn rn if(value.getType() == CellType.LABEL)rn rn LabelCell labelvalue = (LabelCell)value;rn valueStr = labelvalue.getString();rn rn ExcelMap.put(keyStr,valueStr );//存储中文-英文rn starrow ++ ;//递增rn rn System.out.println("从Excel文件:< "+this.ExcelPath+" >的sheet:["+sheets[i]+"]读取多语言共<"+starrow+">行");rn //关闭rn rwb.close(); rn rn rn catch(Exception e)rn e.printStackTrace();rn rnrn return ExcelMap ;rn rn rn rn public static void main(String[] args) rn // TODO Auto-generated method stubrn ReadExcel re = new ReadExcel();rn re.setExcelPath("E:/1.xls");rn re.setColumn1(0);//读取列1rn re.setColumn2(1);//读取列 2rn re.setRowStart(1);//读取起始行rn HashMap map = re.ReadExcel();rn Set set = map.keySet();rn Iterator it = set.iterator();rn while(it.hasNext())rn String key = (String)it.next();rn System.out.println(">> "+map.get(key));rn rn rn// setter && getter-------------------startrn public String getExcelPath() rn return ExcelPath;rn rn public void setExcelPath(String excelPath) rn ExcelPath = excelPath;rn rn public HashMap getExcelMap() rn return ExcelMap;rn rnrn public void setExcelMap(HashMap excelMap) rn ExcelMap = excelMap;rn rnrn public int getColumn1() rn return column1;rn rn public void setColumn1(int column1) rn this.column1 = column1;rn rn public int getColumn2() rn return column2;rn rn public void setColumn2(int column2) rn this.column2 = column2;rn rn rn public int getRowStart() rn return rowStart;rn rn public void setRowStart(int rowStart) rn this.rowStart = rowStart;rn rnrn //setter && getter-------------------end rnrnrn[/code]rnrnJXL读取Excel报奇怪空指针错误。是在是没查出原因rn测试Excel满足:至少2列,10行。多个sheet,即一个Excel里有2个以上表格页面 才会出现这个问题rn请问下这个应该怎么解决?rnrn获取sheet名称后,我遍历sheet居然不成功,高手指教下rnrn在线等待 满意即给分 论坛

没有更多推荐了,返回首页