CSV to XLXS format with data in java [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have an .csv file in which data is in below format
TEST;"TEST1";"TEST2";"TEST3";"TEST4" in each column.
I need to convert .csv file to .xlsx file in which each value should in different column. eg:See attached image.
I tried using Apache POI however, its just converting into .xlsx format but data remains in one column.
Can you anyone share sample code.
Sample input in csv
Below is the sample output result which should in xlsx format.

Here is a simple example (without exception handling, encoding, file paths, ...) that could handle CSV with semicolons (in that case csv translates to "character separated file") and creates a Xslx file:
//open input file
BufferedReader br = new BufferedReader(new FileReader("input.csv"));
//create sheet
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet();
//read from file
String line = br.readLine();
for (int rows=0; line != null; rows++) {
//create one row per line
Row row = sheet.createRow(rows);
//split by semicolon
String[] items = line.split(";");
//ignore first item
for (int i=1, col=0; i<items.length; i++) {
//strip quotation marks
String item = items[i].substring(1, items[i].length()-1);
Cell cell = row.createCell(col++);
//set item
cell.setCellValue(item);
}
//read next line
line = br.readLine();
}
//write to xlsx
FileOutputStream out = new FileOutputStream("Output.xlsx");
wb.write(out);
//close resources
br.close();
out.close();
Given an input.csv like this:
TEST;"TEST1";"TEST2";"TEST3";"TEST4"
TEST;"TEST5";"TEST6";"TEST7";"TEST8"
the Output.xlsx looks like this:

Related

Convert PDF to CSV or EXCEL

I am trying to convert PDF file to CSV or EXCEL format.
Here is the code I use to convert to CSV format:
public void convert() throws Exception {
PdfReader pdfReader = new PdfReader("example.pdf");
PdfDocument pdf = new PdfDocument(pdfReader);;
int pages = pdf.getNumberOfPages();
FileWriter csvWriter = new FileWriter("student.csv");
for (int i = 1; i <= pages; i++) {
PdfPage page = pdf.getPage(i);
String content = PdfTextExtractor.getTextFromPage(page);
String[] splitContents = content.split("\n");
boolean isTitle = true;
for (int j = 0; j < splitContents.length; j++) {
if (isTitle) {
isTitle = false;
continue;
}
csvWriter.append(splitContents[j].replaceAll(" ", " "));
csvWriter.append("\n");
}
}
csvWriter.flush();
csvWriter.close();
}
This code works correctly, but the fact is that the CSV format groups rows without taking into account existing columns (some of them are empty), so I would like to convert this file (PDF) to EXCEL format.
The PDF file itself is formed as a table.
What do I mean about spaces. For example, in a PDF file, in a table
| name | some data | | | some data 1 | |
+----------+----------------+------------+-------------+-------------------+--------------+
After converting to a CSV file, the line looks like this:
name some data some data 1
How can I get the same result as a PDF table?
I'd suggest to use PDFBox, like here: Parsing PDF files (especially with tables) with PDFBox
or another library that will allow you to check the data in the Table point by point, and will allow you to create a table by column width (something like Table table = page.getTable(dividers)); ).
If the width of the columns changes, you'll have to implement it based on the headers/first data column ([e.g. position.x of the last character of the first word] minus [position.x of the first character of the new word] - you'll have to figure it out yourself), it's hard so you could make it hardcoded in the beginning. Using Foxit Reader PDF App you can easily measure column width. Then, if you don't find any data in a particular column, you will be able to add an empty column in the CSV file. I know from my own experience that it is not easy, so I wish you good luck.

Trying to export to a text file. Can not figure out how to add other info between Jtable info

Building a program with Java and am trying to export data to a Text file. I have been working on getting the text doc to come out but I am having problems figuring out how to add text between columns. Right now this code exports the rows in order with a space between each and then each new row gets a ________ to separate it. How can i add things between each column per row. Like an adlib of sort. Like "This is a test and we need {Row1 Column1} and then we need to take {Row1, Column2} and put it in {Row1, Column3}
Im really new to this and I hope one of you can help
public void export() throws IOException
{
File file = new File("/Users/SamBurton/Desktop/Export.txt");
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
for(int i = 0; i < jTable1.getRowCount(); i++){
for(int j = 0; j < jTable1.getColumnCount(); j++){
bw.write(jTable1.getModel().getValueAt(i, j)+" ");
}
bw.write("\n_________\n");
}
bw.close();
fw.close();
JOptionPane.showMessageDialog(null, "Data Exported");
}
EDIT:
This is just a tiny piece of what i need to fill in. I have labels on my code for specific items that transfer to thing on this text document. (the bolded/caps stuff)
<name>**SCENE_SHOT_TAKE**</name>
<rate>
<timebase>**FRAMERATE**</timebase>
<ntsc>**NTSC-T/F**</ntsc>
</rate>
<alphatype>none</alphatype>
<pixelaspectratio>**PIXELASPECTRATIO-T/F**</pixelaspectratio>
<anamorphic>**ANAMORPHIC-T/F**</anamorphic>
<file id="file-**X+1**">
Im trying to talk this out the best i can. I need certain cells in my jtable to line up with the CAPS parts of this output document. So each cell corresponds to a fill in. Then at after each row this same thing above goes again with the info from the next row down in the table

IText Unable to read whitespace from tabular data from PDF using Java

This question is already asked but the query i have is not answered. i have a pdf with table in which some columns are not having any values. I need to read those blank spaces.
I have used Itext pdf for extracting data from pdf but while reading the data from table it is read col by col and the column having no value is not read with white spaces but the next column is read.
I have customized LocationTextExtractionStrategy and have overridden getResultantText()
In below image if there is no value for MD and TD col 1,2,3 then while reading the PDF after 1 it is not giving me spaces but giving the next value that is 2. Is there any solution for this to read the blank spaces
PdfReader reader = new PdfReader(filename);
FontRenderFilter fontFilter = new FontRenderFilter();
TextExtractionStrategy strategy = new FilteredTextRenderListener(new MyLocationTextExtractionStrategy(),fontFilter);
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
String finalText = PdfTextExtractor.getTextFromPage(reader, i, strategy);
System.out.println("finalText.." + finalText);
}

Java - Read all pdf file in the folder [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
try
{
PdfReader reader = new PdfReader(RESULT1);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT2));
AcroFields form = stamper.getAcroFields();
String name = form.getField("Text1");//Check Box 1
stamper.close();
reader.close();
FileInputStream file = new FileInputStream(new File("//Users//"+ usr +"//Desktop//TNA//input//FR-OPS-030 Master Training Plan_Rev4.xls"));
HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheet("Sheet1");// getSheetAt(0);
HSSFRow row = sheet.createRow((short) 0);
HSSFCellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(HSSFColor.DARK_BLUE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFRow row1 = sheet.createRow(7);
HSSFCell name_c1 = row1.createCell(0);
name_c1.setCellValue(name);
name_c1.setCellStyle(style);
file.close();
FileOutputStream outFile =new FileOutputStream(new File("//Users//"+ usr +"//Desktop//TNA//output//FR-OPS-030 Master Training Plan_Rev41w.xls"));
workbook.write(outFile);
outFile.close();
}
catch(Exception ex)
{
System.out.println(ex);
}
I used the code to read PDF file from the folder. It's working fine for single document but I have multiple files in the folder. How do I read PDF multiple files. Please advice
You could start by taking a look at java.io.File which has methods for listing files...
File#listFiles which allows you to list ALL the files within the context of the given File instance
File#listFiles(FileFilter) which allows you to filter the list as it's being created...
Or, if you're using Java 7+, you could take a look at the enhanced File I/O API

append data into xlsx file through java

I am using Apache POI for writing into .xlsx file. I can write into .xlsx file but I am unable to append new content. How can I append new content in the .xlsx file?
My Code is:
public static void write(){
try {
Workbook[] wbs = new Workbook[]{new XSSFWorkbook()};
Workbook workbook=wbs[0];
org.apache.poi.ss.usermodel.Sheet sheet = workbook.createSheet();
System.out.println(sheet.getSheetName());
Row row = sheet.createRow(2);
for(int i=0;i<10;i++){
Cell cell=row.createCell(i);
cell.setCellValue("Sun System");
}
FileOutputStream fout=new FileOutputStream("D:/Test.Xlsx");
workbook.write(fout);
fout.close();
} catch (Exception e) {
}
}
The first thing U've to do :
When you're working with Excel 2007 format, its more wise to use XSSF-Implementations, because you've used abstract implementations. Always remember this when using any implementation.
To append to an existing file you need to reach the end of the rows in that particular workbook sheet. This can be achieved by:
int rows = sheet.getPhysicalNumberOfRows(); // or sheet.getLastRowNum();
After that you can create new cells with the XSSF- Implementation classes. For more information refer to this page
You should open the existing file instead of creating a new one if you want to append, see also this stackoverflow question:
Edit existing excel files using jxl api / Apache POI
You are creating a new workbook each time this is run youd want to create a FileInputStream with a file path to the excel file and then use that input stream to get the XSSF workbook you want to edit
FileInputStream fis = new FileInputStream(filePath);
XSSFWorkbook workBook = new XSSFWorkbook(fis);
then in order to get a specific sheet you simply just use the .getSheet or getSheetAt methods that are apart of workBook. then create and set cells

Categories

Resources