求助execl表格的分割问题

zouzouting560 2013-01-29 07:39:51
想实现的功能是某一份execl文件,有一个单元格的值有多个,在其它单元格不变的情况下,分为多行
效果如:
姓名 年龄 爱好
lei 20 游泳,睡觉
lei2 20 玩电脑
变为:
姓名 年龄 爱好
lei 20 游泳
lei 20 睡觉
lei2 20 玩电脑
求高手帮忙!~
...全文
75 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
失落夏天 2013-01-29
  • 打赏
  • 举报
回复
你试下,应该没什么问题,当然参数没用你给的参数,你调一调,应该没问题。
失落夏天 2013-01-29
  • 打赏
  • 举报
回复
*该类的功能是某一份execl文件,有一个单元格的值有多个,在其它单元格不变的情况下,分为多行
 * 效果如:
 * 姓名     年龄        爱好
 * lei   20    游泳,睡觉
 * lei2  20    玩电脑 
 * 变为:
 * 姓名     年龄        爱好
 * lei   20    游泳
 * lei   20    睡觉
 * lei2  20    玩电脑 
 *  
 * */
public class OneRowToMany {
	public static void main(String[] args) {
		//源execl文件
		String fromexecl="D:\\xxx.xlsx";
		//写入到的txt文件,以,分割
		String totxt="D:\\xxx.txt";
		
		
		try {
			String[][] str=ExeclHelper.poiReader(fromexecl);
			int rownum=str.length;
			int columnum=str[0].length;
			
			String result = "";
			//从第1行开始读取数据,第0行是表头
			
			for(int i=1;i<rownum;i++){
				String rows = "";
				//获取除了最后一行之外其他单元格的数据
				for(int j=0;j<columnum-1;j++){
					//获取每行的其他值
					rows=rows+str[i][j]+",";
				}
				System.out.println(str[i][columnum-1]);
				String brandname=str[i][columnum-1];
				//如果正常的以"   "分割的,执行,否侧,跳过   
				if(brandname.contains("   ")){
					String[] brand=brandname.split("   ");
					for(int k=0;k<brand.length;k++){
						result=result+rows+brand[k]+"\n";
					}
				}else{
					result=result+rows+"null"+"\n";
				}
				WriteHelper.writeToTxt(totxt, result);
				
			}	
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


	public static void poiWrite(String[][] str,String filepath){
		InputStream inp;
		 ifexist(filepath);
		try {
			inp = new FileInputStream(filepath);
			int rownum=str.length;
			int columnum=str[0].length;
		
			Workbook wb = WorkbookFactory.create(inp);
        	Sheet sheet = wb.getSheetAt(0);
        	for(int i=0;i<rownum;i++){
        		//System.out.println("i:"+i);
        		Row row = sheet.createRow(i);
        		for(int j=0;j<columnum;j++){
        			/*System.out.println("j:"+j);    			*/
		        	Cell cell=row.createCell(j);       	    	
		        	//设置格式
		        	cell.setCellType(Cell.CELL_TYPE_STRING);
		        	//设置值
	        		cell.setCellValue(str[i][j]);     		
        		}
        	}
        	// Write the output to a file
        	FileOutputStream fileOut = new FileOutputStream(filepath);
        	wb.write(fileOut);
        	fileOut.close();
        	inp.close();   
        	System.out.println("写入完成");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	
	
	//传入文件的地址,判断文件是否存在,如果不存在的话创建该文件
	public static void ifexist(String path){
		try {
			File file=new File(path);
			if(!file.exists()){
			System.out.println("文件不存在,创建该文件,文件地址为:"+path);
			file.createNewFile();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}


public class WriteHelper {
	public static void writeToTxt(String file,String str){
		File f=new File(file);
		if(!f.exists()){
			 try {
				f.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		FileWriter fw;
		try {
			
			fw = new FileWriter(file);
			PrintWriter pw=new PrintWriter(fw);
			pw.print(str);
			pw.flush();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("记录文件写入已完成,文件地址为"+file);
	}
}

失落夏天 2013-01-29
  • 打赏
  • 举报
回复
我尝试下,看看半小时内能不能写出来。。。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧