Come creare stile cellulare verticale con Apache POI?

Recentemente ho incontrato una domanda: devo esportre un excel (xlsx) con java, che deve contenere questo tipo di cella:

immettere qui la descrizione dell'immagine

Ho fatto un file excel con questo text verticale ed esportto come file xml. Poi ho scoperto che lo stile ha un attributo denominato "VerticalText":

immettere qui la descrizione dell'immagine

Per esperienza ho scelto il POI Apache. Ma non ho trovato alcun modo per generare lo stile cellulare con POI. Ho potuto trovare solo il metodo di rotazione, che non potrebbe soddisfare il requisito.

Quindi ho letto più codice di POI e ho scoperto che le cellstyles sono costruite da alcuni file xsb, che non contengono anche text verticale.

Qualsiasi aiuto è molto apprezzato.

L'XML nella tua image è Excel 2003 SpreadsheetML. Ma un file *.xlsx è un archivio ZIP contenente file di Office Open XML. In questo archivio ZIP gli styles.xml contengono:

 ... <cellXfs count="2"> ... <xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"> <alignment textRotation="255"/> </xf> </cellXfs> ... <cellXfs count = "2"> ... <cellXfs count="2"> ... <xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"> <alignment textRotation="255"/> </xf> </cellXfs> ... <xf numFmtId = "0" fontId = "0" fillId = "0" borderId = "0" xfId = "0"> ... <cellXfs count="2"> ... <xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"> <alignment textRotation="255"/> </xf> </cellXfs> ... <alignment textRotation = "255" /> ... <cellXfs count="2"> ... <xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"> <alignment textRotation="255"/> </xf> </cellXfs> ... 

Ci <alignment textRotation="255"/> è per il text verticale.

Questo può essere impostato usando apache poi come così:

 import java.io.FileOutputStream; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class CreateXSSFVerticalText { public static void main(String[] args) throws Exception { Workbook workbook = new XSSFWorkbook(); CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setRotation((short)255); Sheet sheet = workbook.createSheet(); Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("test"); cell.setCellStyle(cellStyle); FileOutputStream fileOut = new FileOutputStream("CreateXSSFVerticalText.xlsx"); workbook.write(fileOut); fileOut.close(); workbook.close(); } } import java.io.FileOutputStream; import java.io.FileOutputStream; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class CreateXSSFVerticalText { public static void main(String[] args) throws Exception { Workbook workbook = new XSSFWorkbook(); CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setRotation((short)255); Sheet sheet = workbook.createSheet(); Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("test"); cell.setCellStyle(cellStyle); FileOutputStream fileOut = new FileOutputStream("CreateXSSFVerticalText.xlsx"); workbook.write(fileOut); fileOut.close(); workbook.close(); } } import org.apache.poi.ss.usermodel. *; import java.io.FileOutputStream; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class CreateXSSFVerticalText { public static void main(String[] args) throws Exception { Workbook workbook = new XSSFWorkbook(); CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setRotation((short)255); Sheet sheet = workbook.createSheet(); Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("test"); cell.setCellStyle(cellStyle); FileOutputStream fileOut = new FileOutputStream("CreateXSSFVerticalText.xlsx"); workbook.write(fileOut); fileOut.close(); workbook.close(); } } class pubblica CreateXSSFVerticalText { import java.io.FileOutputStream; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class CreateXSSFVerticalText { public static void main(String[] args) throws Exception { Workbook workbook = new XSSFWorkbook(); CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setRotation((short)255); Sheet sheet = workbook.createSheet(); Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("test"); cell.setCellStyle(cellStyle); FileOutputStream fileOut = new FileOutputStream("CreateXSSFVerticalText.xlsx"); workbook.write(fileOut); fileOut.close(); workbook.close(); } } public static void main (String [] args) getta l'exception { import java.io.FileOutputStream; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class CreateXSSFVerticalText { public static void main(String[] args) throws Exception { Workbook workbook = new XSSFWorkbook(); CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setRotation((short)255); Sheet sheet = workbook.createSheet(); Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("test"); cell.setCellStyle(cellStyle); FileOutputStream fileOut = new FileOutputStream("CreateXSSFVerticalText.xlsx"); workbook.write(fileOut); fileOut.close(); workbook.close(); } } Workbook workbook = nuovo XSSFWorkbook (); import java.io.FileOutputStream; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class CreateXSSFVerticalText { public static void main(String[] args) throws Exception { Workbook workbook = new XSSFWorkbook(); CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setRotation((short)255); Sheet sheet = workbook.createSheet(); Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("test"); cell.setCellStyle(cellStyle); FileOutputStream fileOut = new FileOutputStream("CreateXSSFVerticalText.xlsx"); workbook.write(fileOut); fileOut.close(); workbook.close(); } } CellStyle cellStyle = workbook.createCellStyle (); import java.io.FileOutputStream; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class CreateXSSFVerticalText { public static void main(String[] args) throws Exception { Workbook workbook = new XSSFWorkbook(); CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setRotation((short)255); Sheet sheet = workbook.createSheet(); Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("test"); cell.setCellStyle(cellStyle); FileOutputStream fileOut = new FileOutputStream("CreateXSSFVerticalText.xlsx"); workbook.write(fileOut); fileOut.close(); workbook.close(); } } Foglio di foglio = workbook.createSheet (); import java.io.FileOutputStream; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class CreateXSSFVerticalText { public static void main(String[] args) throws Exception { Workbook workbook = new XSSFWorkbook(); CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setRotation((short)255); Sheet sheet = workbook.createSheet(); Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("test"); cell.setCellStyle(cellStyle); FileOutputStream fileOut = new FileOutputStream("CreateXSSFVerticalText.xlsx"); workbook.write(fileOut); fileOut.close(); workbook.close(); } } Riga fila = sheet.createRow (0); import java.io.FileOutputStream; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class CreateXSSFVerticalText { public static void main(String[] args) throws Exception { Workbook workbook = new XSSFWorkbook(); CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setRotation((short)255); Sheet sheet = workbook.createSheet(); Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("test"); cell.setCellStyle(cellStyle); FileOutputStream fileOut = new FileOutputStream("CreateXSSFVerticalText.xlsx"); workbook.write(fileOut); fileOut.close(); workbook.close(); } } Cell cellulare = row.createCell (0); import java.io.FileOutputStream; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class CreateXSSFVerticalText { public static void main(String[] args) throws Exception { Workbook workbook = new XSSFWorkbook(); CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setRotation((short)255); Sheet sheet = workbook.createSheet(); Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("test"); cell.setCellStyle(cellStyle); FileOutputStream fileOut = new FileOutputStream("CreateXSSFVerticalText.xlsx"); workbook.write(fileOut); fileOut.close(); workbook.close(); } } FileOutputStream fileOut = nuovo FileOutputStream ("CreateXSSFVerticalText.xlsx"); import java.io.FileOutputStream; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class CreateXSSFVerticalText { public static void main(String[] args) throws Exception { Workbook workbook = new XSSFWorkbook(); CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setRotation((short)255); Sheet sheet = workbook.createSheet(); Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("test"); cell.setCellStyle(cellStyle); FileOutputStream fileOut = new FileOutputStream("CreateXSSFVerticalText.xlsx"); workbook.write(fileOut); fileOut.close(); workbook.close(); } } 

Poiché i formati Office Open XML (come *.xlsx ) sono archivi ZIP contenenti file XML, è abbastanza facile determinare gli attributi XML necessari. Basta creare un semplice file *.xlsx con la formattazione necessaria utilizzando la GUI di Excel . Poi estraete il file *.xlsx e xl/styles.xml un'occhiata a xl/styles.xml .