I want to create a java project which converts excel file into csv file. Show steps to…
- User
I want to create a java project which converts excel file into csv file. Show steps to create the project in intellij with maven as dependency manager and project should be industtry level,, with folder structure
- Assistant
- User
showing erros near package imports
- Assistant
Sure, I can help you with that. Here's an example of how your Java file should look like:
javapackage com.example.exceltocsv; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import org.apache.poi.ss.usermodel.Cell; 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.xssf.usermodel.XSSFWorkbook; public class ExcelToCsvConverter { public static void convert(String excelFilePath, String csvFilePath) throws IOException { Workbook workbook = new XSSFWorkbook(new FileInputStream(new File(excelFilePath))); Sheet sheet = workbook.getSheetAt(0); FileWriter csvWriter = new FileWriter(new File(csvFilePath)); for (Row row : sheet) { for (Cell cell : row) { String cellValue = cell.getStringCellValue(); csvWriter.append(cellValue + ","); } csvWriter.append("\n"); } csvWriter.flush(); csvWriter.close(); workbook.close(); } } - User
Cannot get a STRING value from a NUMERIC cell
- Assistant
The error message "Cannot get a STRING value from a NUMERIC cell" occurs when you try to read a cell that contains a numeric value as a string using the
getStringCellValue()method.To fix this error, you can use the
DataFormatterclass from Apache POI library to format the cell values based on their data types.Here's an updated example of the
convert()method that uses theDataFormatterclass:javapublic static void convert(String excelFilePath, String csvFilePath) throws IOException { Workbook workbook = new XSSFWorkbook(new FileInputStream(new File(excelFilePath))); Sheet sheet = workbook.getSheetAt(0); FileWriter csvWriter = new FileWriter(new File(csvFilePath)); DataFormatter dataFormatter = new DataFormatter(); for (Row row : sheet) { for (Cell cell : row) { String cellValue = dataFormatter.formatCellValue(cell); csvWriter.append(cellValue + ","); } csvWriter.append("\n"); } csvWriter.flush(); csvWriter.close(); workbook.close(); } - User
I want to read evrything as string
- Assistant
If you want to read every cell value as a string, regardless of its data type, you can modify the
convert()method as follows:javapublic static void convert(String excelFilePath, String csvFilePath) throws IOException { Workbook workbook = new XSSFWorkbook(new FileInputStream(new File(excelFilePath))); Sheet sheet = workbook.getSheetAt(0); FileWriter csvWriter = new FileWriter(new File(csvFilePath)); for (Row row : sheet) { for (Cell cell : row) { String cellValue = cell.toString(); csvWriter.append(cellValue + ","); } csvWriter.append("\n"); } csvWriter.flush(); csvWriter.close(); workbook.close(); }In this example, we simply called the
toString()method on each cell to convert its value to a string. This will work for most cases, but it's worth noting that some Excel cell values may not convert to a string as expected. For example, if a cell contains a formula, calling on it will return the formula rather than the resulting value. In such cases, you may need to use a more specialized approach to read the cell value. - User
Code works fine. Now i want to develop a mapping in informatica developer 10.5.2, which reades a excel file and uses above code to generate a csv file.
- Assistant
To create a mapping in Informatica Developer that reads an Excel file and uses the Java code to generate a CSV file, you can follow these steps:
- Create a new mapping by right-clicking on the folder in which you want to create the mapping and selecting "New Mapping" from the context menu.
- Drag a "Source" object from the toolbar onto the mapping canvas.