I have a code where my client send an excel file to the server. The server (SpringBoot)
needs to 'translate' the MultiplartFile
to an excel File.
From then, the data needs to be inserted into a database.
However, I never need to generate an excel but rather should insert the data from the spreadsheet into the database directly.
I first tried with :
@RequestMapping(value = "/insert", method = RequestMethod.POST, consumes = "multipart/form-data")
@ResponseBody
public MyMessage insertExcell(@RequestPart("typeFile") String typeFile,
@RequestPart("uploadFile") MultipartFile multipart, @RequestPart("dataUser") DataUser dataUser) {
BufferedReader br;
List<String> result2 = new ArrayList<String>();
try {
String line;
InputStream is = multipart.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
result2.add(line);
}
} catch (Exception e) {
}
for (int i = 0; i < result2.size(); i++) {
System.out.println("sentence" + result2.get(i));;
}
The output returns strange symbols.
Then again i tried with :
InputStream inputStream;
try {
inputStream = multipart.getInputStream ();
BufferedReader bufferedReader = new BufferedReader (new InputStreamReader (inputStream));
String line;
while ((line = bufferedReader.readLine()) != null)
{
System.out.println("linea era" + line);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
The console output shows strange symbols.
How can i read the data from the excel file uploaded ?
Click on the Databases tab, and drop your Excel file in the Select Table column. This will launch a window that lets you verify your column names. If it's all looking good, give the table a name, and click on Create Table. You're done!
From what i understand, you need to read an Excel file, get your data then save it into a database.
Excel files are stored in various formats :
If you simply try to read such a file, this would be quite a task...
Fortunately, Apache POI has a library that should help.
You can download it here.
Here is a simple example on how to read your excel file :
try (InputStream inputStream = multipartFile.getInputStream())
{
Workbook wb = WorkbookFactory.create(inputStream);
// opening the first sheet
Sheet sheet = wb.getSheetAt(0);
// read the third row
Row row = sheet.getRow(2);
// read 4th cell
Cell cell = row.getCell(3);
// get the string value
String myValue = cell.getStringCellValue();
// store in the database...
} catch (IOException e) {
//TODO
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With