Tag: line catch blank string save book work contents comma
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;
public class exchange{
public static void Main (string[] args) {
Find the path to the Excel file
String filepath = "D:\\demo.xls";
try {
Workbook Workbook = Workbook.getworkbook (new File (filepath));
Sheet Sheet = workbook.getsheet (0);
Turn txt to save the location and file name
File File = new file ("D:/1.txt");
FileWriter FW = new FileWriter (file);
BufferedWriter bw = new BufferedWriter (FW);
J is the number of rows, Getcell ("column number", "line number")
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 ();
Each column is separated by commas, and the last edge of the first column and the last column are not comma
if (x!=0 && Stringutils.isnotblank (s))
Bw.write ("," +s);
Else
Bw.write (s);
Bw.flush ();
}
Bw.newline ();
Bw.flush ();
}
System.out.println ("Write End");
} catch (Biffexception e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
Illustration 1: Mainly uses the input and output of the file operation, as well as the method that obtains the row and the column in the Jxl.jar and the reading date and the output date format.
Note 2: Generally we configure the jar package or Maven repository is not jxl.jar this jar package, but the conversion needs to use so we need to add the jar package ourselves.
How to add: Add Dependencies in the Maven pom.xml of the owning project, as follows:
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.10</version>
</dependency>
After adding the dependencies, update maven,maven will automatically download Jxl.jar the jar package.
After the MAVEN warehouse update is complete, the jar package is ready for import, and the Getcell (), getcontents (), etc. in the JXL package can be used.
Implementing Excel to TXT with Java