lr10 -> task_2

This commit is contained in:
2026-04-05 21:23:04 +05:00
parent 500c41df80
commit 598306fb7a
5 changed files with 415 additions and 0 deletions

58
lr10/task_2/e3.java Normal file
View File

@@ -0,0 +1,58 @@
package lr10.task_2;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class e3 {
public static void main(String[] args) {
try {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Товары");
Row headerRow = sheet.createRow(0);
String[] columns = { "Название товара", "Характеристики", "Стоимость" };
for (int i = 0; i < columns.length; i++) {
Cell cell = headerRow.createCell(i);
cell.setCellValue(columns[i]);
}
// Добавляем данные чисто тестовые данные, так для вижу
String[][] data = {
{ "Книга", "Автор: Иванов, Год: 2023", "1000" },
{ "Компьютер", "Процессор: Intel Core i7, RAM: 16GB", "50000" }
};
int rowNum = 1;
for (String[] rowData : data) {
Row row = sheet.createRow(rowNum++);
int colNum = 0;
for (String value : rowData) {
Cell cell = row.createCell(colNum++);
cell.setCellValue(value);
}
}
try (FileOutputStream outputStream = new FileOutputStream("output.xlsx")) {
workbook.write(outputStream);
}
workbook.close();
System.out.println("Данные успешно записаны в файл output.xlsx");
} catch (FileNotFoundException e) {
System.err.println("Файл не найден: " + e.getMessage());
} catch (IOException e) {
System.err.println("Ошибка при записи данных в файл: " + e.getMessage());
} catch (Exception e) {
System.err.println("Произошла непредвиденная ошибка: " + e.getMessage());
}
}
}