csv 형식 변환 xls

2320 단어 Excel 관련
/** 
 *  excel csv xls 
 * @author hanchuang 
 * */
public class CSVToExcelConverter {
	

	public static void CSVToExcel(String filename,String url) throws IOException {
		ArrayList arList = null;
		ArrayList al = null;
		
		String fName = url+filename;
		
	
		
		String thisLine;
		int count = 0;
		FileInputStream fis = new FileInputStream(fName);
		DataInputStream myInput = new DataInputStream(fis);
		int i = 0;
		arList = new ArrayList();
		while ((thisLine = myInput.readLine()) != null) {
			al = new ArrayList();
			String strar[] = thisLine.split(",");
			for (int j = 0; j < strar.length; j++) {
				al.add(strar[j]);
			}
			arList.add(al);
			System.out.println();
			i++;
		}

		try {
			HSSFWorkbook hwb = new HSSFWorkbook();
			HSSFSheet sheet = hwb.createSheet("new sheet");
			for (int k = 0; k < arList.size(); k++) {
				ArrayList ardata = (ArrayList) arList.get(k);
				HSSFRow row = sheet.createRow((short) 0 + k);
				for (int p = 0; p < ardata.size(); p++) {
					HSSFCell cell = row.createCell((short) p);
//					String data = ardata.get(p).toString();
					// getBytes(String charsetName):   byte  ,  byte  。
					String data = new String(ardata.get(p).toString().getBytes("ISO-8859-1"), "gbk");
					
					if (data.startsWith("=")) {
						cell.setCellType(Cell.CELL_TYPE_STRING);
						data = data.replaceAll("\"", "");
						data = data.replaceAll("=", "");
						cell.setCellValue(data);
					} else if (data.startsWith("\"")) {
						data = data.replaceAll("\"", "");
						cell.setCellType(Cell.CELL_TYPE_STRING);
						cell.setCellValue(data);
					} else {
						data = data.replaceAll("\"", "");
						cell.setCellType(Cell.CELL_TYPE_NUMERIC);
						cell.setCellValue(data);
					}
					// */
					// cell.setCellValue(ardata.get(p).toString());
				}
				
			}
			String[] fNames=filename.split("csv");
			String newFilename="";
			System.out.println(fNames.length);
			for(int n=0;n

좋은 웹페이지 즐겨찾기