
package com.javamap.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
public class SimpleReadExcel {
public static void main(String[] args) throws EncryptedDocumentException, IOException {
try {
Workbook workbook = WorkbookFactory.
create(new FileInputStream("./data/data.xlsx"));
// System.out.println("Workbook has " + workbook.getNumberOfSheets() + " Sheets : ");
Sheet sheet = workbook.getSheetAt(0);
// System.out.println("\n\nIterating over Rows and Columns using Iterator\n");
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// Now let's iterate over the columns of the current row
int len = row.getLastCellNum();
for ( int i = 0; len > i ; i++) {
System.out.print(row.getCell(i).toString());
if(len-1 == i)
{
// print nothing
}
else
{
System.out.print(",");
}
}
System.out.println();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
package com.test.javamap;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import com.sap.aii.mapping.api.AbstractTrace;
import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
public class JavaMapExcelToCSV extends AbstractTransformation {
private final static String LINE_FEED = "\r\n";
private boolean isRunningInEclipse = false;
private String fileInput = null;
@Override
public void transform(TransformationInput in, TransformationOutput out) throws StreamTransformationException {
AbstractTrace trace = this.getTrace();
InputStream is = null;
OutputStream os = null;
if (in != null) {
is = in.getInputPayload().getInputStream();
os = out.getOutputPayload().getOutputStream();
} else {
try {
is = new FileInputStream(this.fileInput);
this.isRunningInEclipse = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
// throw your exception
}
}
try {
Workbook workbook = WorkbookFactory.create(is);
Sheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
StringBuilder sb = new StringBuilder();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// Now let's iterate over the columns of the current row
int len = row.getLastCellNum();
for (int i = 0; len > i; i++) {
// System.out.print(row.getCell(i).toString());
sb.append(row.getCell(i).toString());
if (len - 1 == i) {
// print nothing
} else {
sb.append(",");
}
}
sb.append(LINE_FEED);
}
System.out.println(sb.toString());
os.write(sb.toString().getBytes());
} catch (Exception exception) {
trace.addDebugMessage(exception.getMessage());
throw new StreamTransformationException(exception.toString());
}
}
public static void main(String[] args) throws StreamTransformationException {
JavaMapExcelToCSV excelToCSV = new JavaMapExcelToCSV();
excelToCSV.fileInput = "./data/data.xlsx";
excelToCSV.transform((TransformationInput) null, (TransformationOutput) null);
}
}
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
/*
// use this for local testing groovy script
def is = new FileInputStream("Your File name here")
def output = convertExcelToCSV(is, null)
// print output here
*/
def Message processData(Message message) {
// logging
def messageLog = messageLogFactory.getMessageLog(message);
if (messageLog != null) {
messageLog.setStringProperty("log1", "starting")
;
}
// def body = message.getBody(java.lang.String)
def body = message.getBody(java.io.InputStream)
def output = convertExcelToCSV(body, messageLog)
message.setBody(output)
return message
}
def String convertExcelToCSV(def is, def messageLog) throws Exception {
if (messageLog != null) {
// Add your logging here
messageLog.setStringProperty("Log2", "in Method convertExcelToCSV, starting conversion")
}
StringBuilder sb = new StringBuilder()
def LINE_FEED = "\r\n"
try {
Workbook workbook = WorkbookFactory.create(is)
Sheet sheet = workbook.getSheetAt(0)
Iterator<Row> rowIterator = sheet.iterator()
while (rowIterator.hasNext()) {
Row row = rowIterator.next()
// Now let's iterate over the columns of the current row
int len = row.getLastCellNum();
for (int i = 0; len > i; i++) {
// System.out.print(row.getCell(i).toString());
sb.append(row.getCell(i).toString());
if (len - 1 == i) {
// print nothing
} else {
sb.append(",");
}
}
sb.append(LINE_FEED);
}
//System.out.println(sb.toString());
if (messageLog != null) {
// Add your logging here
messageLog.setStringProperty("Log3", "in Method convertExcelToCSV, finished covnersion")
messageLog.addAttachmentAsString("CSV Payload", sb.toString(), "text/csv");
}
} catch (Exception exception) {
throw new RuntimeException(exception.toString());
}
return sb.toString()
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
11 | |
10 | |
9 | |
8 | |
6 | |
6 | |
6 | |
5 | |
5 | |
5 |