Implementing Excel to TXT with Java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.