Untitled
unknown
plain_text
a year ago
3.7 kB
7
Indexable
package com.example.demo.Controller;
import com.example.demo.Model.ExcelReading;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@Controller
@RequestMapping("/upload")
public class UploadController {
private static final String UPLOAD_DIR = "./uploads/";
@Autowired
private ExcelReading excelReading;
@PostMapping
public String handleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
if (file.isEmpty()) {
redirectAttributes.addFlashAttribute("message", "Yüklenecek dosya bulunamadı.");
return "redirect:/";
}
try {
// Klasörün var olup olmadığını kontrol edin, yoksa oluşturun
Path uploadPath = Paths.get(UPLOAD_DIR);
if (!Files.exists(uploadPath)) {
Files.createDirectories(uploadPath);
}
// Dosyayı yükleme dizinine kaydedin
byte[] bytes = file.getBytes();
Path path = uploadPath.resolve(file.getOriginalFilename());
Files.write(path, bytes);
// Dosyayı okuyun ve içeriklerini işleyin
try (InputStream inputStream = file.getInputStream()) {
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {
switch (cell.getCellType()) {
case STRING:
System.out.print(cell.getStringCellValue() + "\t");
break;
case NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t");
break;
case BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t");
break;
case FORMULA:
System.out.print(cell.getCellFormula() + "\t");
break;
default:
System.out.print("DEFAULT\t");
}
}
System.out.println();
}
}
// ExcelReading sınıfını kullanarak dosya verilerini işleyin
excelReading.readExcelData(path.toString());
System.out.println("Kolon Başlıkları: " + excelReading.getColumnHeaders());
System.out.println("Kolon Başlıkları 2: " + excelReading.getColumnHeaders2());
redirectAttributes.addFlashAttribute("message", "Dosya başarıyla yüklendi ve işlendi!");
return "redirect:/selection";
} catch (IOException e) {
e.printStackTrace();
redirectAttributes.addFlashAttribute("message", "Dosya yükleme sırasında bir hata oluştu.");
return "redirect:/";
}
}
}
Editor is loading...
Leave a Comment