默認情況下,Excel存儲全局字符串(每個工作簿1個)SharedStringTablePart
。所以,這是你需要的目標。但是,OpenXML格式還允許在WorksheetParts中嵌入文本。因此,一個完整的解決方案也需要在那裏查看。
下面是一個示例應用程序(有一些在線評論):
using DocumentFormat.OpenXml.Packaging;
using x = DocumentFormat.OpenXml.Spreadsheet;
class Program
{
private static readonly string placeHolder = "$$value";
static void Main()
{
var templatePath = @"C:\Temp\template.xlsx";
var resultPath = @"C:\Temp\result.xlsx";
string replacementText = "test";
using (Stream xlsxStream = new MemoryStream())
{
// Read template from disk
using (var fileStream = File.OpenRead(templatePath))
fileStream.CopyTo(xlsxStream);
// Do replacements
ProcessTemplate(xlsxStream, replacementText);
// Reset stream to beginning
xlsxStream.Seek(0L, SeekOrigin.Begin);
// Write results back to disk
using (var resultFile = File.Create(resultPath))
xlsxStream.CopyTo(resultFile);
}
}
private static void ProcessTemplate(Stream template, string replacementText)
{
using (var workbook = SpreadsheetDocument.Open(template, true, new OpenSettings { AutoSave = true }))
{
// Replace shared strings
SharedStringTablePart sharedStringsPart = workbook.WorkbookPart.SharedStringTablePart;
IEnumerable<x.Text> sharedStringTextElements = sharedStringsPart.SharedStringTable.Descendants<x.Text>();
DoReplace(sharedStringTextElements, replacementText);
// Replace inline strings
IEnumerable<WorksheetPart> worksheetParts = workbook.GetPartsOfType<WorksheetPart>();
foreach (var worksheet in worksheetParts)
{
var allTextElements = worksheet.Worksheet.Descendants<x.Text>();
DoReplace(allTextElements, replacementText);
}
} // AutoSave enabled
}
private static void DoReplace(IEnumerable<x.Text> textElements, string replacementText)
{
foreach (var text in textElements)
{
if (text.Text.Contains(placeHolder))
text.Text = text.Text.Replace(placeHolder, replacementText);
}
}
嗨ServiceGuy感謝您的回覆......這個問題我從你的代碼面對的我不能保存使用「新的OpenSettings的{自動保存= true}」其獲取參考問題的「OpenSetting」..是否有任何其他方式將工作簿另存爲另一個工作簿,將其作爲模板... – kart 2011-05-09 14:06:09
@ kart:我更新示例代碼喲你的要求(加載模板並保存到新文件)。另外,請確保使用OpenXML SDK 2.0的RTM:http://www.microsoft.com/downloads/en/details.aspx?FamilyID=c6e744e5-36e9-45f5-8d8c-331df206e0d0&displaylang=en – mthierba 2011-05-09 15:18:08
@ServiceGuy:謝謝soo多爲您的指導......在調試時,我能夠在DoReplace函數中找到文本替換,但這些替換的文本未顯示在新創建的Excel文件中。對我來說,我無法添加OpenSetting「SpreadsheetDocument.Open(template,true,new OpenSettings {AutoSave = true})」第三個參數不允許在「SpreadsheetDocument.Open()」方法中允許它只允許兩個參數。 .still我不能夠取代新創建的文件中的實際數據..它創建相同的模板文件。 – kart 2011-05-10 09:19:49