java實(shí)現(xiàn)excel和txt文件互轉(zhuǎn)
話不多說(shuō),請(qǐng)看代碼:
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
{
//打開(kāi)文件
WritableWorkbook book= Workbook.createWorkbook(new File("測(cè)試.xls"));
//生成名為“第一頁(yè)”的工作表,參數(shù)0表示這是第一頁(yè)
WritableSheet sheet=book.createSheet("第一頁(yè)",0);
//在Label對(duì)象的構(gòu)造子中指名單元格位置是第一列第一行(0,0)
//以及單元格內(nèi)容為test
Label label=new Label(0,0,"test");
//將定義好的單元格添加到工作表中
sheet.addCell(label);
/*生成一個(gè)保存數(shù)字的單元格
必須使用Number的完整包路徑,否則有語(yǔ)法歧義
單元格位置是第二列,第一行,值為789.123*/
jxl.write.Number number = new jxl.write.Number(1,0,789.123);
sheet.addCell(number);
//寫(xiě)入數(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("寫(xiě)入結(jié)束");
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
遇到的問(wèn)題:
txt文件中單元格數(shù)據(jù)之間用|分割,用string.split("\\|");提取數(shù)據(jù)
用的jar包對(duì)excel2007不支持 從而導(dǎo)致轉(zhuǎn)換出的是空文件
excel文件轉(zhuǎn)txt文件時(shí),用tab鍵分隔 分隔字符串?dāng)?shù)組時(shí)用String.split("\\ ",-1);
上線遇到的問(wèn)題:
1.在windows上獲取路徑地址是以\分隔的,而在linux上獲取的路徑是以/分隔的,這要注意
2.默認(rèn)情況下,Excel中每個(gè)單元格所能顯示的數(shù)字為11位,輸入超過(guò)11位的數(shù)值,系統(tǒng)自動(dòng)將其轉(zhuǎn)換為科學(xué)記數(shù)格式,當(dāng)txt轉(zhuǎn)excel時(shí),有兩種方法可以解決這個(gè)問(wèn)題,第一種是在單元格數(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)過(guò)嘗試,使用GBK解析文件,用以下代碼可不出現(xiàn)亂碼,
BufferedReader bw = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filen)),"GBK"));
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家
相關(guān)文章
Spring?Boot中的JdbcClient與JdbcTemplate使用對(duì)比分析
這篇文章主要介紹了Spring Boot中的JdbcClient與JdbcTemplate使用對(duì)比分析,一起看看Spring Boot 中 JdbcClient 和 JdbcTemplate 之間的差異2024-01-01
Springboot使用@WebListener?作為web監(jiān)聽(tīng)器的過(guò)程解析
這篇文章主要介紹了Springboot使用@WebListener作為web監(jiān)聽(tīng)器的過(guò)程,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08
SpringBoot中mapper.xml文件存放的兩種實(shí)現(xiàn)位置
這篇文章主要介紹了SpringBoot中mapper.xml文件存放的兩種實(shí)現(xiàn)位置,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
Spring依賴(lài)注入中的@Resource與@Autowired詳解
這篇文章主要介紹了Spring依賴(lài)注入中的@Resource與@Autowired詳解,提到Spring依賴(lài)注入,大家最先想到應(yīng)該是@Resource和@Autowired,對(duì)于Spring為什么要支持兩個(gè)這么類(lèi)似的注解卻未提到,屬于知其然而不知其所以然,本文就來(lái)做詳細(xì)講解,需要的朋友可以參考下2023-09-09
多用多學(xué)之Java中的Set,List,Map詳解
下面小編就為大家?guī)?lái)一篇多用多學(xué)之Java中的Set,List,Map詳解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06

