In: Computer Science
public class Test1{
int MAXLEN=100000;
static
void convert(File inputFile, File outputFile) {
try {
FileOutputStream fos = new FileOutputStream(outputFile);
// Get the workbook object for XLSX file
XSSFWorkbook wBook = new XSSFWorkbook(
new FileInputStream(inputFile));
// Get first sheet from the workbook
XSSFSheet sheet = wBook.getSheetAt(0);
Row row;
Cell cell;
// Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
row = rowIterator.next();
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
data.append(cell.getBooleanCellValue() + ",");
break;
case Cell.CELL_TYPE_NUMERIC:
data.append(cell.getNumericCellValue() + ",");
break;
case Cell.CELL_TYPE_STRING:
data.append(cell.getStringCellValue() + ",");
break;
case Cell.CELL_TYPE_BLANK:
data.append("" + ",");
break;
default:
data.append(cell + ",");
}
}
}
fos.write(data.toString().getBytes());
fos.close();
} catch (Exception
ioe) {
ioe.printStackTrace();
}
}
public String readExcel(String pathtocsv
){
String[] ans=new
String[MAXLEN];
BufferedReader csvReader = new
BufferedReader(new FileReader(pathtocsv));
int lineNumber=1;
while ((row = csvReader.readLine())
!= null) {
String[]
data = row.split(",");
// do
something with the data
String
binary = new BigInteger(text.getBytes()).toString(2);
ans[lineNumber++]=binary;
}
csvReader.close();
return ans;
}
public static void main(String[] args) {
//I know to read csv file, that
is why I convert it to csv first
//if you have xlsx file, then you
need to convert it in csv. Else skip this
File inputFile = new
File("/home/Desktop/Test.xlsx");
File outputFile = new
File("/home/Desktop/Test1.csv");
convert(inputFile,
outputFile);
//we can read
pathtocsv="/home/Desktop/Test1.csv";
String[] ans=readExcel(pathtocsv
);
//ans is our answer
}
}