Код: Выделить всё
//here a easy example
Main(string[] args)
{
CloseAllOfficeProcess(); // close all
var Locations = new List { "l1", "l2", "l3" }; // different regions
string OutputDir = @"E:\test\out\";
string WordTemplatePath = @"E:\test\test.docx";
await SyncWordExcelData(WordTemplatePath, OutputDir, Locations, 3);
}
async Task SyncWordExcelData(string WordTemplatePath, string OutputDir, List Locations, int maxParallelism)
{
Application wordApp = new Application();
try
{
var partitions = Locations
.Select((location, index) => new { location, index })
.GroupBy(x => x.index % maxDegreeOfParallelism)
.Select(g => g.Select(x => x.location).ToList())
.ToList();
var tasks = new List();
foreach (var partition in partitions)
{
tasks.Add(Task.Run(async () =>
{
foreach (var location in partition)
{
Document docCopy = CreateTemporaryDocument(WordTemplatePath, wordApp, OutputDir);
await ProcessWord(docCopy, OutputDir, location);
docCopy.Save();
docCopy.Close(false);
}
}));
}
await Task.WhenAll(tasks);
}
finally
{
wordApp.Quit();
}
}
static Document CreateTemporaryDocument(string templatePath, Application wordApp, string outputDir)
{
string tempFilePath = Path.Combine(outputDir, Guid.NewGuid() + ".docx");
File.Copy(templatePath, tempFilePath, overwrite: true);
return wordApp.Documents.Open(tempFilePath);
}
static async Task ProcessWord(Document Doc, string OutputDir, string location)
{
foreach(Bookmark bookmark in Doc.Bookmarks)
{
string bookmarkName = bookmark.Name;
Word.Range bookmarkRange = bookmark.Range;
object rangeObj = bookmarkRange;
InlineShape inlineShape = Doc.InlineShapes.AddChart((Microsoft.Office.Core.XlChartType)Excel.XlChartType.xlColumnClustered, ref rangeObj);
Word.Chart chart = inlineShape.Chart;
chart.HasTitle = false;
chart.HasLegend = false;
}
await Task.CompletedTask;
}
Сейчас я использую
Office2019
Microsoft.Office .Interop.Excel
Microsoft.Office.Interop.Word
Подробнее здесь: https://stackoverflow.com/questions/793 ... ing-charts
Мобильная версия