您好,登錄后才能下訂單哦!
話不多說,請看代碼:
import java.io.*; import jxl.*; import jxl.write.*; //用java將txt數(shù)據(jù)導(dǎo)入excel public class CreateXLS { public static void main(String args[]) { try { //打開文件 WritableWorkbook book= Workbook.createWorkbook(new File("測試.xls")); //生成名為“第一頁”的工作表,參數(shù)0表示這是第一頁 WritableSheet sheet=book.createSheet("第一頁",0); //在Label對象的構(gòu)造子中指名單元格位置是第一列第一行(0,0) //以及單元格內(nèi)容為test Label label=new Label(0,0,"test"); //將定義好的單元格添加到工作表中 sheet.addCell(label); /*生成一個(gè)保存數(shù)字的單元格 必須使用Number的完整包路徑,否則有語法歧義 單元格位置是第二列,第一行,值為789.123*/ jxl.write.Number number = new jxl.write.Number(1,0,789.123); sheet.addCell(number); //寫入數(shù)據(jù)并關(guān)閉文件 book.write(); book.close(); }catch(Exception e) { System.out.println(e); } } }
import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; //用java將excel數(shù)據(jù)導(dǎo)入txt public class WriteTxt { public static void main(String[] args) { // TODO Auto-generated method stub String filepath = "d:\\demo.xls"; try { Workbook workbook = Workbook.getWorkbook(new File(filepath)); Sheet sheet = workbook.getSheet(0); File file = new File("d:/1.txt"); FileWriter fw = new FileWriter(file); BufferedWriter bw = new BufferedWriter(fw); // j為行數(shù),getCell("列號(hào)","行號(hào)") int j = sheet.getRows(); int y = sheet.getColumns(); for (int i = 0; i < j; i++) { for(int x=0; x<y; x++){ Cell c = sheet.getCell(x, i); String s = c.getContents(); bw.write(s); bw.write(" "); bw.flush(); } bw.newLine(); bw.flush(); } System.out.println("寫入結(jié)束"); } catch (BiffException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
遇到的問題:
txt文件中單元格數(shù)據(jù)之間用|分割,用string.split("\\|");提取數(shù)據(jù)
用的jar包對excel2007不支持 從而導(dǎo)致轉(zhuǎn)換出的是空文件
excel文件轉(zhuǎn)txt文件時(shí),用tab鍵分隔 分隔字符串?dāng)?shù)組時(shí)用String.split("\\ ",-1);
上線遇到的問題:
1.在windows上獲取路徑地址是以\分隔的,而在linux上獲取的路徑是以/分隔的,這要注意
2.默認(rèn)情況下,Excel中每個(gè)單元格所能顯示的數(shù)字為11位,輸入超過11位的數(shù)值,系統(tǒng)自動(dòng)將其轉(zhuǎn)換為科學(xué)記數(shù)格式,當(dāng)txt轉(zhuǎn)excel時(shí),有兩種方法可以解決這個(gè)問題,第一種是在單元格數(shù)字前加個(gè)單引號(hào),第二種是設(shè)置單元格的格式為文本格式,在上述代碼中加入以下代碼
WritableFont wf = new WritableFont(WritableFont.TIMES,12,WritableFont.NO_BOLD,false); WritableCellFormat wcfF = new WritableCellFormat(NumberFormats.TEXT); wcfF.setFont(wf); CellView cv = new CellView(); cv.setFormat(wcfF); cv.setSize(10*265); sheet.setColumnView(j, cv); Label label = new Label(j,n,s1[j]); sheet.addCell(label);
3. 當(dāng)txt轉(zhuǎn)excel在windows上轉(zhuǎn)換成功時(shí),到linux服務(wù)器上轉(zhuǎn)出的excel中漢字變成了亂碼,因?yàn)?code>FileWriter fw = new FileWriter(file);這句代碼采用默認(rèn)字符集解析,經(jīng)過嘗試,使用GBK解析文件,用以下代碼可不出現(xiàn)亂碼,
BufferedReader bw = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filen)),"GBK"));
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持億速云
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。