Skip to content
This repository has been archived by the owner on Jul 26, 2018. It is now read-only.

To get the code for writing input text files to excel file #42

Open
RejiKumar3333 opened this issue May 14, 2018 · 0 comments
Open

To get the code for writing input text files to excel file #42

RejiKumar3333 opened this issue May 14, 2018 · 0 comments

Comments

@RejiKumar3333
Copy link

Can u please help me to get the code for writing the given input text files and it should be written to an Excel file.Text file to Excel file is the simplest question of mine.I have give certain codes to get but it not works properly.Below I will copy my written coding.

package utils;

import android.database.Cursor;
import android.os.Environment;
import android.util.Log;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;

import jxl.CellView;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.UnderlineStyle;
import jxl.read.biff.File;
import jxl.write.Formula;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import java.io.*;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class WriteExcel {
String input,output,engine,from,to;
ArrayList al= new ArrayList();

public boolean createfile(String input,String output,String engine,String from, String to) {
    this.input=input;
    this.output=output;
    this.engine=engine;
    this.from=from;
    this.to=to;

    Excelget write1 =new Excelget();
    write1.setInput(input);
    write1.setOutput(output);
    write1.setEngine(engine);
    write1.setFrom(from);
    write1.setTo(to);

        al.add(write1);





    try {

        String fpath = "/sdcard/" + "output" + ".xls";

        java.io.File file1 = new java.io.File(fpath);
        createExcelSheet(file1);


        // If file does not exists, then create it
        if (!file1.exists()) {
            file1.createNewFile();
        }





        return true;

    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }

}
java.io.File file;
WritableWorkbook workbook;

    public void createExcelSheet(java.io.File file) {

        this.file=file;
                    WorkbookSettings wbSettings = new WorkbookSettings();
        wbSettings.setLocale(new Locale("en", "EN"));
        try {
            workbook = Workbook.createWorkbook(file, wbSettings);
            createFirstSheet();
            //closing cursor
            workbook.write();
            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    void createFirstSheet() {
        try {

            //Excel sheet name. 0 (number)represents first sheet
            WritableSheet sheet = workbook.createSheet("sheet1", 0);
            // column and row title
            sheet.addCell(new Label(0, 0, "Original text"));
            sheet.addCell(new Label(1, 0, "Translated text"));
            sheet.addCell(new Label(2, 0, "Engine"));
            sheet.addCell(new Label(3, 0, "Translated from"));
            sheet.addCell(new Label(4, 0, "Translated to"));


            for(Excelget i:al) {

                sheet.addCell(new Label(0, 1, i.getInput()));
                sheet.addCell(new Label(0, 2, i.getOutput()));
                sheet.addCell(new Label(0, 3, i.getEngine()));
                sheet.addCell(new Label(0, 4, i.getFrom()));
                sheet.addCell(new Label(0, 5, i.getTo()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant