Using Java to achieve the translation of Excel and txt files

Source: Internet
Author: User



import java.io.*;
import jxl.*;
import jxl.write.*;

//import TXT data into Excel with Java
Public class Createxls
{
Public static void Main (String args[])
{
Try
{
//Open File
Writableworkbook book= Workbook.createworkbook (New File ("Test. xls"));
//Generate a worksheet named "first page", parameter 0 indicates this is the first page
Writablesheet Sheet=book.createsheet ("first page", 0);
//In the construction child of the label object, the cell position is the first column, the first row (0,0)
//And cell content is test
label Label=new label (0,0, "test");
//Add a defined cell to the worksheet
Sheet.addcell (label);
/ * Generate a cell that holds numbers
the full package path of number must be used, otherwise there is a syntax ambiguity
The cell position is the second column, the first row, and the value is 789.123*/
jxl.write.Number number = new Jxl.write.Number (1,0,789.123);
Sheet.addcell (number);
//write data and close file
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;

Importing Excel data into TXT with Java

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 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 ();
Bw.write (s);
bw.write ("");
Bw.flush ();
}
bw.newline ();
Bw.flush ();
}
System.out.println ("Write End");
} catch (Biffexception e) {
e.printstacktrace ();
} catch (IOException e) {
e.printstacktrace ();
}
}
}



Problems encountered: TXT file in the cell data between the use of | split, with string.split ("\\|"); Extracting data

Using Java to achieve the translation of Excel and txt files

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.