CSV(逗号分隔值)文件是各行业数据交换的核心载体 — 从数据分析到后端系统均有广泛应用。它体积轻巧、易于人工阅读,且兼容几乎所有工具(Excel、谷歌表格、数据库等)。对于需要在 Python 中创建 CSV 文件的开发者来说,Spire.XLS for Python 是优选。
本文将介绍如何使用 Spire.XLS 在 Python 中生成 CSV 文件,涵盖基础 CSV 创建以及列表转 CSV、Excel 转 CSV 等进阶使用场景。
内容概览:
Spire.XLS for Python 兼容 Python 3.6+,安装仅需1行命令,无复杂依赖:
pip install Spire.XLS
如需解锁全部功能(如处理超大文件),可申请临时免费许可证,无限制测试全部功能。
先从简单场景入手:使用静态数据(例如销售报表)从头创建 CSV 文件。以下代码会创建一个新的工作簿,填充数据后将其保存为 CSV 文件。
from spire.xls import *
# 1. 初始化工作簿和工作表
workbook = Workbook()
worksheet = workbook.Worksheets[0] # 获取默认第一个工作表
# 2. 填充数据(表头+内容)
# 写入表头(字符串类型用Text属性)
worksheet.Range["A1"].Text = "编号"
worksheet.Range["B1"].Text = "名称"
worksheet.Range["C1"].Text = "价格"
worksheet.Range["D1"].Text = "售出数"
# 写入数据行(数字类型用NumberValue属性,保证数据格式正确)
worksheet.Range["A2"].NumberValue = 101
worksheet.Range["B2"].Text = "无线耳机"
worksheet.Range["C2"].NumberValue = 79.99
worksheet.Range["D2"].NumberValue = 250
worksheet.Range["A3"].NumberValue = 102
worksheet.Range["B3"].Text = "蓝牙音箱"
worksheet.Range["C3"].NumberValue = 49.99
worksheet.Range["D3"].NumberValue = 180
# 3. 导出为CSV文件(指定分隔符为逗号,编码UTF-8)
worksheet.SaveToFile("基础销售报表.csv", ",", Encoding.get_UTF8())
# 释放资源,避免内存泄漏
workbook.Dispose()
核心逻辑解析:
Workbook() 创建空工作簿,Worksheets[0] 定位到第一个工作表(CSV 本质是单工作表文件);Text 属性:用于字符串(如表头、产品名称),避免数字被自动格式化;NumberValue 属性:用于数值(如价格、销量),保证 CSV 导入 Excel/数据库时类型正确;SaveToFile() 将工作表导出为 CSV 格式,Dispose() 必须调用以释放内存。输出结果:
生成的基础销售报表.csv内容如下:

在实际业务场景中,数据通常存储在字典中(例如来自 API 或数据库)。以下代码将字典列表转换为 CSV 文件:
from spire.xls import *
# 模拟动态数据(如从API/数据库获取)
客户数据 = [
{"客户ID": 1, "姓名": "张三", "邮箱": "zhang@ example.com", "国家": "中国"},
{"客户ID": 2, "姓名": "Maria Garcia", "邮箱": "maria@ example.es", "国家": "西班牙"},
{"客户ID": 3, "姓名": "王五", "邮箱": "wang@ example.cn", "国家": "中国"}
]
# 1. 初始化工作簿
workbook = Workbook()
worksheet = workbook.Worksheets[0]
# 2. 自动提取表头(从第一个字典的键生成)
表头 = list(客户数据[0].keys())
for 列索引, 列名 in enumerate(表头, start=1):
worksheet.Range[1, 列索引].Text = 列名 # 第1行固定为表头
# 3. 批量写入数据行
for 行索引, 客户 in enumerate(客户数据, start=2): # 从第2行开始写数据
for 列索引, 键 in enumerate(表头, start=1):
数值 = 客户[键]
# 自动判断数据类型,避免格式错误
if isinstance(数值, (int, float)):
worksheet.Range[行索引, 列索引].NumberValue = 数值
else:
worksheet.Range[行索引, 列索引].Text = 数值
# 4. 导出CSV(UTF-8编码兼容中文)
worksheet.SaveToFile("客户数据.csv", ",", Encoding.get_UTF8())
workbook.Dispose()
该示例非常适用于 JSON 转 CSV、数据库导出以及 REST API 数据导出场景。核心优势包括:
输出结果:

日常工作中常需将 Excel 报表(XLS/XLSX)转为 CSV 用于数据导入,Spire.XLS 可一键转换,支持批量处理多工作表。
from spire.xls import *
# 1. 初始化工作簿实例
workbook = Workbook()
# 2. 加载xlsx文件
workbook.LoadFromFile("示例1.xlsx")
# 3. 将Excel另存为CSV文件
workbook.SaveToFile("XLSX转CSV.csv", FileFormat.CSV)
workbook.Dispose()
转换结果:

注意事项: 默认情况下,SaveToFile() 仅转换第一个工作表。如需将多个工作表转换为单独的 CSV 文件,可遍历每个工作表并一一转换为单独的 CSV 文件。
遵循以下准则,避免数据格式错误、乱码、内存溢出等问题:
编码必选UTF-8
始终指定 Encoding.get_UTF8(),兼容中文、特殊字符,避免不同系统间乱码。
数据预处理
导出前清理空行、空值、特殊符号(如换行符、逗号),防止 CSV 解析错误。
超大文件分块处理
若数据量超10万行,避免一次性加载,可按批次写入。
适配不同分隔符
欧洲地区常用分号(;),制表符分隔(TSV)用\t,仅需修改 SaveToFile 的第二个参数:
# 分号分隔(适配欧洲系统)
worksheet.SaveToFile("欧洲版数据.csv", ";", Encoding.get_UTF8())
# 制表符分隔(TSV格式)
worksheet.SaveToFile("TSV格式数据.csv", "\t", Encoding.get_UTF8())
workbook.Dispose(),尤其循环处理多个文件时,防止内存泄漏。答:csv 模块适合基础读写,但 Spire.XLS 具备多项显著优势:
csv 模块默认全部转为字符串)答:可以。Spire.XLS 支持解析 CSV 文件并提取其中的数据。详情可参考:Python 读取 CSV 文件:从基础到进阶指南
答:完全支持,以下是极简示例:
from spire.xls import *
workbook = Workbook()
# 加载CSV文件
workbook.LoadFromFile("客户数据.csv", ",", 1, 1)
# 另存为Excel格式
workbook.SaveToFile("CSV转Excel.xlsx", ExcelVersion.Version2016)
workbook.Dispose()
答: SaveToFile() 方法的第二个参数用于控制分隔符。
Spire.XLS 简化了使用 Python 生成 CSV 文件的流程。无论你是从头创建报表、转换 Excel 工作簿,还是处理来自 API 和数据库的动态数据,该库都能提供健壮且灵活的解决方案。
基于本文示例,开发者可以轻松自定义分隔符、指定 UTF-8 等编码格式,并管理数据类型 — 确保你的 CSV 文件准确、兼容,可直接用于各类应用场景。如需了解更多进阶功能,可参阅官方教程合集。
Spire.XLS for Python 16.2.0 现已正式发布。该版本新增了对 ARM 架构 macOS 的支持。同时,一些在复制 OLE 对象、删除宏、将文本格式数值转换为数字格式以及将 Excel 文档保存为 EMF 图片时出现的问题也得以成功被修复。更多详情如下。
新功能:
问题修复:
Spire.Presentation for Python 11.2.0 现已正式发布。该版本新增了对 ARM 架构 macOS 的支持。同时,修复了在将 PPT 转换为图片时出现的内容丢失问题。详情如下。
新功能:
问题修复:
https://www.e-iceblue.cn/Downloads/Spire-Presentation-Python.html
我们很高兴地宣布 Spire.Office 11.1.0 正式发布。在这个版本中,Spire.Doc 支持在 Word 文件中创建和操作 VBA 宏;Spire.XLS 支持将指定的单元格区域转换为 HTML;Spire.Presentation 支持基于正则表达式匹配结果对文本进行高亮显示;Spire.PDF 支持将文件保存为 PDF 2.0 版本。此外,本次更新还成功修复了大量已知问题。
该版本涵盖了最新版的 Spire.Doc、Spire.PDF、Spire.XLS、Spire.Presentation、Spire.Barcode、Spire.DocViewer、Spire.PDFViewer 和 Spire.Email。
https://www.e-iceblue.cn/Downloads/Spire-Office-NET.html
新功能:
HtmlExportOptions options = doc.HtmlExportOptions;
options.OfficeMathOutputMode = HtmlOfficeMathOutputMode.MathML;
doc.RemoveBlankPages(); //删除空白页
doc.RemovePages(new List {0,1,3});//删除指定页
Document doc = new Document();
doc.AddSection().AddParagraph().AppendText("wertyuiop[]fghjk");
//添加VBA项目到文档
VbaProject vbaProject = new VbaProject();
vbaProject.Name = "SampleVBAMacro";
doc.VbaProject = vbaProject;
//将模块添加到VBA项目
//模块1
VbaModule vbaModule1 = doc.VbaProject.Modules.Add("SampleModule1", VbaModuleType.StdModule);
vbaModule1.SourceCode = @"
Sub DocumnetInfo()
MsgBox ""create time: "" &Now()
MsgBox ""Pages:"" & ActiveDocument.Range.ComputeStatistics(wdStatisticPages)
End Sub
Sub WriteHello()
Selection.TypeText Text:=""Hello World!""
End Sub";
//模块2
VbaModule vbaModule2 = doc.VbaProject.Modules.Add("SampleModule2", VbaModuleType.StdModule);
vbaModule2.SourceCode = @"
Sub InsertCurrentDate()
Selection.TypeText Text:=Format(Now(),""yyyy-mm-dd hh:mm:ss"")
End Sub
Sub IndentParagraph()
Selection.ParagraphFormat.LeftIndent = InchesToPoints(0.5)
End Sub";
doc.SaveToFile("result.docm", FileFormat.Docm);
doc.Close();
Document doc = new Document();
doc.LoadFromFile("input.docx");
StringBuilder sb = new StringBuilder();
RevisionInfoCollection revisionInfoCollection = doc.GetRevisionInfos();
foreach (RevisionInfo revisionInfo in revisionInfoCollection)
{
sb.AppendLine("[author]:" + revisionInfo.Author + "\r\n" + " [RevisionType]:" + revisionInfo.RevisionType + "\r\n" + " [DateTime]:" + revisionInfo.DateTime.ToString() + "\r\n" + " [OwnerObject]:" + revisionInfo.OwnerObject + "\r\n" + " [OwnerObject.Owner]:" + revisionInfo.OwnerObject.Owner + "\r\n");
if (revisionInfo.OwnerObject is TextRange textRange)
{
TextRange range = (TextRange)textRange;
sb.AppendLine($"作用对象:文本范围 - 内容:{range.Text}");
}
}
File.WriteAllText(outputFile, sb.ToString());
doc.Dispose();
问题修复:
新功能:
// 使用BYROW计算每行的平均分
sheet.Range["G2"].Formula = "=BYROW(B2:F2, LAMBDA(row, AVERAGE(row)))";
// 使用BYCOL计算每列的平均值
sheet.Range["B8"].Formula = "=BYCOL(B2:B7, LAMBDA(col, AVERAGE(col)))";
Workbook workbook = new Workbook();
workbook.LoadFromFile(inputFile);
Worksheet sheet = workbook.Worksheets[0];
CellRange cell = sheet.Range["A1:B3"];
string html = cell.HtmlString;
File.WriteAllText(outputFile, html);
CopyRangeOptions options = CopyRangeOptions.Transpose | CopyRangeOptions.All;
sheet["A1:C4"].Copy(sheet["D2:G3"], options);
sheet["A1:B5"].Copy(sheet["D5"], options);
workbook.SaveToFile(outputFile);
Workbook workbook = new Workbook();
workbook.LoadFromFile(inputFile);
workbook.ConverterSetting.PdfConformanceLevel = Spire.Xls.Pdf.PdfConformanceLevel.Pdf_UA1;
workbook.SaveToFile(outputFile, FileFormat.PDF);
workbook.Dispose();
问题修复:
新功能:
CopyRegex regex = new Regex(@"\bhello\b");
IAutoShape shape = (IAutoShape)ppt.Slides[0].Shapes[0];
TextHighLightingOptions options = new TextHighLightingOptions();
shape.TextFrame.HighLightRegex(regex, Color.Red, options);
问题修复:
新功能:
// 获取
PdfFileInfo info = doc.FileInfo;
PdfVersion version = info.Version;
//设置
doc.FileInfo.Version = PdfVersion.Version2_0;
问题修复:
Spire.Office for Java 11.1.0 现已正式发布。在该版本中,Spire.Doc for Java 支持为表格应用自定义样式;Spire.XLS for Java 支持插入、获取、更新 Equation 公式;Spire.Presentation for Java 支持从形状中读取自定义数据;Spire.PDF for Java 支持将 PDF 对比结果保存到文件流 。此外,该版本还修复了许多已知问题。详细更新内容如下。
https://www.e-iceblue.cn/Downloads/Spire-Office-JAVA.html
优化:
ListStyle listStyle = document.getStyles().add(ListType.Numbered, "levelstyle");
listStyle.isCustomStyle(true);
ListLevelCollection levels = listStyle.getListRef().getLevels();
levels.get(0).setPatternType(ListPatternType.Arabic);
levels.get(0).setStartAt(1);
levels.get(0).getCharacterFormat().setFontName("Trebuchet MS");
paragraph.getListFormat().applyStyle(ListStyle listStyle);
paragraph.getListFormat().setListLevelNumber(int leverNumber)
或
paragraph.getListFormat().applyListRef(ListDefinitionReference list, int leverNumber);
新功能:
Document document = new Document();
Section sec = document.addSection();
Paragraph paragraph = sec.addParagraph();
// 创建默认项目符号列表模板
ListTemplate template = ListTemplate.Bullet_Default;
ListDefinitionReference listRef = document.getListReferences().add(template);
// 创建默认编号列表模板
ListTemplate template1 = ListTemplate.Number_Default;
listRef = document.getListReferences().add(template1);
listRef.getLevels().get(2).setStartAt(4);// 设置第3级列表从数字4开始
// 添加段落并应用编号列表样式(第2级)
paragraph = sec.addParagraph();
paragraph.appendText("List Item 1");
// 应用第2级(索引从0开始,1表示第2级)
paragraph.getListFormat().applyListRef(listRef, 1);
paragraph = sec.addParagraph();
paragraph.appendText("List Item 2");
// 应用第3级
paragraph.getListFormat().applyListRef(listRef, 2);
paragraph = sec.addParagraph();
paragraph.appendText("List Item 3");
// 应用第2级
paragraph.getListFormat().applyListRef(listRef, 1);
paragraph = sec.addParagraph();
paragraph.appendText("List Item 4");
// 应用第3级
paragraph.getListFormat().applyListRef(listRef, 2);
document.saveToFile(outputFile, FileFormat.Docx);
document.close();
Document document = new Document();
Section sec = document.addSection();
Paragraph paragraph = sec.addParagraph();
// 创建阿拉伯数字编号列表模板(如 "1.", "2.", ...)
ListTemplate template = ListTemplate.Number_Arabic_Dot;
// 使用 addSingleLevelList 创建单级列表引用
ListDefinitionReference listRef = document.getListReferences().addSingleLevelList(template);
int levelcount = listRef.getLevels().getCount();//判断层级
boolean res=listRef.isMultiLevel();
// 添加第一个列表项
paragraph = sec.addParagraph();
paragraph.appendText("List Item 1");
paragraph.getListFormat().applyListRef(listRef, 0);
paragraph = sec.addParagraph();
paragraph.appendText("List Item 2");
paragraph.getListFormat().applyListRef(listRef, 0);
document.saveToFile(outputFile, FileFormat.Docx);
document.close();
Document document = new Document();
Section sec = document.addSection();
Paragraph paragraph = sec.addParagraph();
// 创建自定义项目符号列表样式
ListStyle listStyle = document.getStyles().add(ListType.Bulleted, "bulletList");
// 获取该列表样式的各级别配置
ListLevelCollection Levels = listStyle.getListRef().getLevels();
// 为第1级设置图片项目符号
Levels.get(0).createPictureBullet();
Levels.get(0).getPictureBullet().loadImage(imag_Path_1);
// 为第2级设置图片项目符号
Levels.get(1).createPictureBullet();
Levels.get(1).getPictureBullet().loadImage(imag_Path_2);
// 添加第一个列表项
paragraph = sec.addParagraph();
paragraph.appendText("List Item 1");
paragraph.getListFormat().applyStyle(listStyle);
// 添加第二个列表项
paragraph = sec.addParagraph();
paragraph.appendText("List Item 1.1");
paragraph.getListFormat().applyStyle(listStyle);
paragraph.getListFormat().setListLevelNumber(1);
//删除第1级的图片项目符号
Levels.get(0).deletePictureBullet();
document.saveToFile(outputFile, FileFormat.Docx);
document.close();
document.getStyles().get("style1").removeSelf();
Document doc = new Document();
Section section = doc.addSection();
TableStyle tableStyle = (TableStyle) doc.getStyles().add(StyleType.Table_Style, "TestTableStyle1");
tableStyle.setHorizontalAlignment(RowAlignment.Center);
tableStyle.getBorders().setColor(Color.BLUE);
tableStyle.getBorders().setBorderType(BorderStyle.Single);
Table table = section.addTable();
table.resetCells(1, 1);
table.getRows().get(0).getCells().get(0).addParagraph().appendText("Aligned to the center of the page");
table.setPreferredWidth(PreferredWidth.fromPoints(300));
table.applyStyle(tableStyle);
// table.getFormat().setStyle(tableStyle);
doc.saveToFile(outputDocxFile, FileFormat.Docx);
doc.copyStylesFromTemplate(inputFile_2); // Accepts file path as String
doc.copyStylesFromTemplate(doc2); // Accepts another Document object
问题修复:
新功能:
// 插入Equation
sheet.getEquations().addEquation(11, 0, 100, 100, "x_{1}^{2}");
// 获取Equation
String mathML = sheet.getEquations().get(0).exportMathML();
String LaTex = sheet.getEquations().get(0).exportLaTex();
// 更新Equation
IXlsEquation equation1 = sheet.getEquations().get(0);
equation1.updateByLaTexText("\\begin{pmatrix} \r\n 1 & 0 \\\\ \r\n 0 & 1 \r\n \\end{pmatrix} \r\n \\left(x-1\\right)\\left(x+3\\right) \\left(x-1\\right)\\left(x+3\\right)");
worksheet.getCellRange("A2").setFormula("=F.INV(0.99, 100, 200)");
worksheet.getCellRange("A2").setFormula("=T.INV.2T(0.05, 10)");
问题修复:
新功能:
// Create a new PdfDocument object 'pdf1' to work with the first PDF file
PdfDocument pdf1 = new PdfDocument();
// Load the first PDF file from the specified path
pdf1.loadFromFile("ComparePdfDocument_1.pdf");
// Create a new PdfDocument object 'pdf2' to work with the second PDF file
PdfDocument pdf2 = new PdfDocument();
// Load the second PDF file from the specified path
pdf2.loadFromFile("ComparePdfDocument_2.pdf");
// Create a PdfComparer object 'compare' with 'pdf1' and 'pdf2' as parameters for comparison
PdfComparer compare = new PdfComparer(pdf1, pdf2);
// Set the page ranges to be compared using the options of the comparer
compare.getOptions().setPageRanges(0, pdf1.getPages().getCount() - 1, 0, pdf2.getPages().getCount() - 1);
String result = "output.pdf";
File outFile = new File(result);
// Create an output stream to write the document to the output file
OutputStream outputStream = new FileOutputStream(outFile);
// Compare the PDF documents and save
compare.compare(outputStream);
// Dispose of system resources associated with 'pdf1'
pdf1.dispose();
// Dispose of system resources associated with 'pdf2'
pdf2.dispose();
问题修复与优化:
新功能:
Presentation ppt = new Presentation();
ppt.loadFromFile(inputFile);
List dataList = ppt.getSlides().get(0).getShapes().get(0).getCustomerDataList();
System.out.println(dataList.size());
for(int i = 0; i < dataList.size(); i++)
{
String name = dataList.get(i).getName();
String content = dataList.get(i).getXML();
}
Presentation ppt = new Presentation();
ppt.loadFromFile(inputFile);
Rectangle2D.Double audioRect = new Rectangle2D.Double(220, 240, 80, 80);
IAudio audio=ppt.getSlides().get(0).getShapes().appendAudioMedia(inputFile_1, audioRect);
// 设置音频播放开始时的淡入效果持续时间为 13 秒
audio.setFadeInDuration(13000f);
// 设置音频播放结束时的淡出效果持续时间为 20 秒
audio.setFadeOutDuration(20000f);
ppt.saveToFile(outputFile, FileFormat.PPTX_2016);
ppt.dispose();
Presentation ppt = new Presentation();
ppt.loadFromFile(inputFile);
Rectangle2D.Double audioRect = new Rectangle2D.Double(220, 240, 80, 80);
IAudio audio = ppt.getSlides().get(0).getShapes().appendAudioMedia(inputFile_1, audioRect);
// 设置音频的起始裁剪时间为 8 秒
audio.setTrimFromStart(8000f);
// 设置音频的结束裁剪时间为 13 秒
audio.setTrimFromEnd(13000f);
ppt.saveToFile(outputFile, FileFormat.PPTX_2016);
ppt.dispose();
Presentation presentation = new Presentation();
presentation.loadFromFile("data/test.pptx");
Double[] widths = new Double[]{100d, 100d, 150d, 100d, 100d};
Double[] heights = new Double[]{15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d};
//添加表格
ITable table = presentation.getSlides().get(0).getShapes().appendTable((float) presentation.getSlideSize().getSize().getWidth() / 2 - 275, 90, widths, heights);
//设置范围是1-0,表格默认颜色为黑色
table.getFill().setTransparency(0.5f);
//设置具体表格颜色
table.get(0,0).getFillFormat().setFillType(FillFormatType.SOLID);
table.get(0,0).getFillFormat().getSolidColor().setColor(Color.BLUE);
presentation.saveToFile("result.pptx",FileFormat.PPTX_2016);
问题修复:
Spire.OCR 2.2.2 现已正式发布。该版本新增了对拉丁文字的识别支持,同时优化了中文的识别效果。详细更新内容如下。
新功能:
问题修复:
Spire.Doc for C++ 14.1.4 现已发布,该版本同步了一系列 Spire.Doc for .NET 的功能,主要集中于表格和列表相关的功能。具体更新如下。
新功能:
int firstColumn = doc->GetBookmarks()->FindByName(L"t_insert")->GetFirstColumn();
int lastColumn = doc->GetBookmarks()->FindByName(L"t_insert")->GetLastColumn();
intrusive_ptr<TableStyle> tableStyle = Object::Dynamic_cast<TableStyle>(
doc->GetStyles()->Add(StyleType::TableStyle, L"TestTableStyle1")
);
tableStyle->GetBorders()->SetColor(Color::GetBlue());
tableStyle->GetBorders()->SetBorderType(BorderStyle::Single);
tableStyle->SetHorizontalAlignment(RowAlignment::Center);
intrusive_ptr<Table> table = sec->AddTable();
table->ResetCells(1, 1);
table->GetRows()->GetItemInRowCollection(0)
->GetCells()->GetItemInCellCollection(0)
->AddParagraph()->AppendText(L"Aligned to the center of the page");
table->SetPreferredWidth(PreferredWidth::FromPoints(300));
table->ApplyStyle(tableStyle);
sec->AddParagraph()->AppendText(L"");
tableStyle = Object::Dynamic_cast<TableStyle>(
doc->GetStyles()->Add(StyleType::TableStyle, L"TestTableStyle2")
);
tableStyle->SetLeftIndent(55);
tableStyle->GetBorders()->SetColor(Color::GetGreen());
tableStyle->GetBorders()->SetBorderType(BorderStyle::Single);
table = sec->AddTable();
table->ResetCells(1, 1);
table->GetRows()->GetItemInRowCollection(0)
->GetCells()->GetItemInCellCollection(0)
->AddParagraph()->AppendText(L"Aligned according to left indent");
table->SetPreferredWidth(PreferredWidth::FromPoints(300));
table->GetFormat()->SetStyle(tableStyle);
intrusive_ptr<TableStyle> tableStyle = Object::Dynamic_cast<TableStyle>(
doc->GetStyles()->Add(StyleType::TableStyle, L"TestTableStyle1")
);
tableStyle->GetBorders()->SetColor(Color::GetBlack());
tableStyle->GetBorders()->SetBorderType(BorderStyle::Double);
tableStyle->SetRowStripe(3);
tableStyle->GetConditionalStyles()->GetItem(TableConditionalStyleType::OddRowStripe)
->GetShading()->SetBackgroundPatternColor(Color::GetLightBlue());
tableStyle->GetConditionalStyles()->GetItem(TableConditionalStyleType::EvenRowStripe)
->GetShading()->SetBackgroundPatternColor(Color::GetLightCyan());
tableStyle->SetColumnStripe(1);
tableStyle->GetConditionalStyles()->GetItem(TableConditionalStyleType::EvenColumnStripe)
->GetShading()->SetBackgroundPatternColor(Color::GetLightPink());
table->ApplyStyle(tableStyle);
table->GetFormat()->SetStyleOptions(TableStyleOptions::ColumnStripe);
tableStyle = Object::Dynamic_cast<TableStyle>(
doc->GetStyles()->Add(StyleType::TableStyle, L"TestTableStyle3")
);
tableStyle->SetLeftIndent(55);
tableStyle->GetBorders()->SetColor(Color::GetGreen());
tableStyle->GetBorders()->SetBorderType(BorderStyle::Single);
tableStyle->SetHorizontalAlignment(RowAlignment::Right);
table = sec->AddTable();
table->ResetCells(1, 1);
table->GetRows()->GetItemInRowCollection(0)
->GetCells()->GetItemInCellCollection(0)
->AddParagraph()->AppendText(L"Aligned according to left indent");
table->SetPreferredWidth(PreferredWidth::FromPoints(300));
table->GetFormat()->SetStyleName(L"TestTableStyle3");
document->GetStyles()->GetItem(L"Normal")->RemoveSelf();
intrusive_ptr<Document> doc = ConvertUtil::GetNewEngineDocument();
intrusive_ptr navigator = new DocumentNavigator(doc);
doc->LoadFromFile(inputFile.c_str());
navigator->MoveToDocumentStart();
navigator->Writeln(L"Test insert at DocumentStart");
navigator->Writeln(L"Test insert without move");
navigator->MoveToDocumentEnd();
navigator->Writeln(L"Test insert at DocumentEnd");
doc->SaveToFile(outputFile.c_str(), FileFormat::Docx);
doc->Close();
| 类名 | 新功能 | 功能描述 |
|---|---|---|
| Paragraph | GetText | 获取段落的文本内容 |
| Table | SetBorders, ClearBorders |
设置表格边框样式;清除表格的所有边框格式 |
| CellFormat | ClearFormatting | 清除单元格的所有格式 |
| Borders | ClearFormatting, IsShadow |
清除边框格式设置;控制边框是否显示阴影效果 |
| RowFormat | ClearBackground, Height | 清除行背景色;设置行高 |
| StyleCollection | Add (重载) | 新增样式添加重载 |
| PreferredWidth | FromPercent, FromPoints |
宽度数据类型 |
| CharacterFormat | LocaleIdBi | 支持双向文本的区域设置 |
| Frame | IsFrame | 判断对象是否为 Frame |
| OfficeMath | ToLaTexMathCode, FromOMMLCode |
将公式对象转换为 LaTeX 数学代码;从 OMML 字符串创建公式对象 |
| 类名 | 新功能 | 功能描述 |
|---|---|---|
| CompareOptions | IgnoreTable、IgnoreHeadersAndFooters | 比较时可忽略表格内容、页眉页脚 |
| DifferRevisions | MoveToRevisions、MoveFromRevisions | 获取"移动到"和"从...移动"类型的修订 |
| StructureDocumentTag* (含 Cell/Inline/Row) | RemoveSelfOnly | 仅移除内容控件本身,保留其包含的内容 |
| 类名 | 新功能 | 功能描述 |
|---|---|---|
| ToPdfParameterList | PdfImageCompression、 DigitalSignatureInfo | 保存到PDF时配置图像压缩以及数字签名信息 |
| Document | MarkdownExportOptions、 ListReferences | 支持 Markdown 导出选项及列表引用 |
| 类名 | 新功能 | 功能描述 |
|---|---|---|
| ListFormat | ApplyStyle, ApplyListRef | 支持直接引用和快速设置 |
| ListLevel | Equals, CreatePictureBullet, DeletePictureBullet, PictureBullet | 支持图片项目符号及比较操作 |
| ListStyle | ListRef, BaseStyle | 支持列表引用与基础样式 |
| Document | ListReferences | 获取文档中的列表引用集合 |
Spire.XLS for Java 16.1.3 现已发布。该版本支持插入、获取、更新 Equation 公式,并支持 F.INV 和 T.INV.2T 公式。此外,一些在操作和转换 Excel 文档 (如 HTML 转 Excel、Excel 转 PDF)时出现的问题也已成功被修复。详情查看下文。
新功能:
// 插入Equation
sheet.getEquations().addEquation(11, 0, 100, 100, "x_{1}^{2}");
// 获取Equation
String mathML = sheet.getEquations().get(0).exportMathML();
String LaTex = sheet.getEquations().get(0).exportLaTex();
// 更新Equation
IXlsEquation equation1 = sheet.getEquations().get(0);
equation1.updateByLaTexText("\\begin{pmatrix} \r\n 1 & 0 \\\\ \r\n 0 & 1 \r\n \\end{pmatrix} \r\n \\left(x-1\\right)\\left(x+3\\right) \\left(x-1\\right)\\left(x+3\\right)");
worksheet.getCellRange("A2").setFormula("=F.INV(0.99, 100, 200)");
worksheet.getCellRange("A2").setFormula("=T.INV.2T(0.05, 10)");
问题修复:
Spire.PDF for Java 12.1.4 现已正式发布。该版本新增了将 PDF 对比结果直接保存到文件流的功能。同时,针对 PDF 转换、渲染、打印及稳定性等多个方面的问题进行了修复,并进一步优化了部分功能的性能和布局效果。更多更新内容如下:
新功能:
// Create a new PdfDocument object 'pdf1' to work with the first PDF file
PdfDocument pdf1 = new PdfDocument();
// Load the first PDF file from the specified path
pdf1.loadFromFile("ComparePdfDocument_1.pdf");
// Create a new PdfDocument object 'pdf2' to work with the second PDF file
PdfDocument pdf2 = new PdfDocument();
// Load the second PDF file from the specified path
pdf2.loadFromFile("ComparePdfDocument_2.pdf");
// Create a PdfComparer object 'compare' with 'pdf1' and 'pdf2' as parameters for comparison
PdfComparer compare = new PdfComparer(pdf1, pdf2);
// Set the page ranges to be compared using the options of the comparer
compare.getOptions().setPageRanges(0, pdf1.getPages().getCount() - 1, 0, pdf2.getPages().getCount() - 1);
String result = "output.pdf";
File outFile = new File(result);
// Create an output stream to write the document to the output file
OutputStream outputStream = new FileOutputStream(outFile);
// Compare the PDF documents and save
compare.compare(outputStream);
// Dispose of system resources associated with 'pdf1'
pdf1.dispose();
// Dispose of system resources associated with 'pdf2'
pdf2.dispose();
问题修复与优化:
Spire.Doc for Python 14.1.6 现已正式发布。该版本新增了对 ARM 架构 macOS 系统的支持。同时,修复了在 Python v3.14 环境中获取文档属性以及在 macOS 系统中设置字体大小时出现的异常问题。详情如下
新功能:
问题修复: