xslfTextRun.setFontFamily("仿宋");
好几天没解决了,资料也很少,没人用POI写过ppt吗
去试了一下,实际上字体已经改了
POI操作Excel和Word用的多点吧 如果你考虑其他库可以试试Free Spire.Presentation, jar包地址,文档资料Java 创建、删除、操作 表格
import com.spire.presentation.*;
public class CreateTable {
public static void main(String []args) throws Exception {
String inputFile = "createTable.pptx";
String outputFile = "createTable_result.pptx";
//Create a PPT document
Presentation presentation = new Presentation();
//Load the document from disk
presentation.loadFromFile(inputFile);
Double[] widths = new Double[]{100d, 100d, 100d, 100d};
Double[] heights = new Double[]{15d, 15d, 15d, 15d};
//Add new table to PPT
ITable table = presentation.getSlides().get(0).getShapes().appendTable((float) presentation.getSlideSize().getSize().getWidth() / 2 - 275, 90, widths, heights);
String[][] dataStr = new String[][]
{
{"姓名","年龄","性别","工号" },
{"张三","28","男","0023" },
{"李四","30","男","0024" },
{"王五","26","女","0025" }
};
//Add data to table
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
//Fill the table with data
table.get(j, i).getTextFrame().setText(dataStr[i][j]);
//Set the Font
table.get(j, i).getTextFrame().getParagraphs().get(0).getTextRanges().get(0).setLatinFont(new TextFont("仿宋"));
}
}
//Set the alignment of the first row to Center
for (int i = 0; i < 4; i++) {
table.get(i, 0).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.CENTER);
}
//Set the style of table
table.setStylePreset(TableStylePreset.LIGHT_STYLE_3_ACCENT_1);
//Save the document
presentation.saveToFile(outputFile, FileFormat.PPTX_2010);
}
}