import java.io.FileNotFoundException;
import java.io.FileOutputStream;
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.xssf.usermodel.XSSFWorkbook;
public class Excel {
public static void main(String[] args) {
//1. Erstellen Sie ein leeres Dokument
XSSFWorkbook wb = new XSSFWorkbook();
//2. Erstellen Sie eine leere Tabelle
Blatt = wb.createSheet("neues Blatt");
//3. Erstellen Sie eine Zeile und fügen Sie etwas ein
Zeile row = sheet.createRow((short)0);
//4. Erstellen Sie eine neue Zelle
Zelle cell = row.createCell(0);
//5. Setzen Sie den Wert
cell.setCellValue(1.2);
//Weitere Zellen mit unterschiedlichen Typen hinzufügen
/*int*/row.createCell(1).setCellValue(3);
/*char*/row.createCell(2).setCellValue('c');
/*String*/row.createCell(3).setCellValue("string");
/*boolean*/row.createCell(4).setCellValue(false);
FileOutputStream fileOut;
try {
fileOut = new FileOutputStream("nouveauFichier.xlsx");
wb.write(fileOut);
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//insert in cell F1
cell = row.createCell((short) 6);
cell.setCellValue(new Date());
XSSFCellStyle cellStyle = wb.createCellStyle();
XSSFDataFormat xssfDataFormat = wb.createDataFormat();
//Erstellen eines Datums- und Uhrzeitformats
cellStyle.setDataFormat(xssfDataFormat.getFormat("dd/mm/yyyy h:mm"));
cell.setCellStyle(cellStyle);
/*create new font*/
Font Schriftart = wb.createFont();
//size: 12px
font.setFontHeightInPoints((short)12);
font.setFontName("Kurier neu");
font.setItalic(true);
font.setBold(true);
/*Erstellen eines neuen Stils*/
CellStyle cs = wb.createCellStyle();
cs.setFont(font);
//style Zelle 3(D1)
row.getCell(3).setCellStyle(cs);
/*Hintergrundfarbe ändern*/
XSSFCellStyle csColor=wb.createCellStyle();
csColor.setFillForegroundColor(new XSSFColor(new Color(194, 154, 250)));
csColor.setFillPattern(csCouleur.SOLID_FOREGROUND);
//Anwenden des Stils auf Zelle 3
row.getCell(2).setCellStyle(csColor);
/*Schriftfarbe ändern*/
Schriftart = wb.createFont();
font.setColor((kurz)45);
CellStyle csCF = wb.createCellStyle();
csCF.setFont(font);
//Stil auf Zelle anwenden 0
row.getCell(0).setCellStyle(csCF);
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.ss.util.CellRangeAddress;
public class Fusion{
public static void main(String[] args) throws FileNotFoundException {
Arbeitsmappe wb = new HSSFWorkbook();
Blattblatt = wb.createSheet("sheet1");
Zeile Zeile = sheet.createRow((kurz) 1);
Zelle Zelle = row.createCell((kurz) 1);
cell.setCellValue("Test zum Zusammenführen von Zellen");
sheet.addMergedRegion(new CellRangeAddress(
1, //erste Zeile B2
2, //letzte Zeile B3
1, //erste Spalte C2
2 //letzte Spalte C3
));
/*Mitte*/
cell.getCellStyle().setAlignment((kurz)2);
cell.getCellStyle().setVerticalAlignment((kurz)1);
FileOutputStream fs = null;
try {
fs = new FileOutputStream("testFusion.xlsx");
wb.write(fs);
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Excel_formule {
public static void main(String[] args) {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet-Blatt = wb.createSheet("Durchschnitt");
Zeile Zeile = sheet.createRow((kurz) 0);
row.createCell(0).setCellValue("Quarter1");
row.createCell(1).setCellValue("Quarter2");
row.createCell(2).setCellValue("Quarter3");
row.createCell(3).setCellValue("Quarter4");
row.createCell(4).setCellValue("Durchschnitt");
Zeile row1 = sheet.createRow((kurz) 1);
row1.createCell(0).setCellValue(2);
row1.createCell(1).setCellValue(5);
row1.createCell(2).setCellValue(1);
row1.createCell(3).setCellValue(7);
row1.createCell(4).setCellFormula("(A2+B2+C2+D2)/4");
try {
FileOutputStream out = new FileOutputStream(new File("formuletest.xlsx"));
wb.write(out);
out.close();
System.out.println("Die Excel-Datei wurde erfolgreich erstellt");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.File;Output
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Excel_parcourir {
public static void main(String[] args) throws IOException {
FileInputStream file = new FileInputStream(new File("formuletest.xlsx"));
//Erstellen einer Arbeitsmappeninstanz, die auf die xlsx-Datei verweist
XSSFWorkbook wb = new XSSFWorkbook(file);
XSSFSheet-Blatt = wb.getSheetAt(0);
FormelEvaluator formulaEvaluator =
wb.getCreationHelper().createFormulaEvaluator();
for (Zeilenzeile: Blatt) {//browse rows
for (Zellzelle: Zeile) {//browse columns
//evaluate cell type
switch (formulaEvaluator.evaluateInCell(cell).getCellType())
{
box Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t\t");
Nachlass;
Feld Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t");
Nachlass;
}
}
System.out.println();
}
}
}
Quarter1 Quarter2 Quarter3 Quarter4 Durchschnitt
2.0 5.0 1.0 7.0 3.75
Please disable your ad blocker and refresh the window to use this website.