LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

一個(gè)支持DOCX、PPTX、Html等文件合并、拆分、互相轉(zhuǎn)換的C#開源項(xiàng)目

admin
2023年8月16日 9:29 本文熱度 871

OpenXML是一個(gè)基于XML的Office文檔格式,包括docx、excel、pptx以及圖表等格式,該規(guī)范是由微軟開發(fā)并發(fā)布的。雖然OpenXML功能很強(qiáng)大,但是在實(shí)際開發(fā)過程中,我們還是會(huì)面臨不少困難,畢竟其功能比較基礎(chǔ)。

所以今天給大家推薦一個(gè)使用 Open XML 文檔(DOCX、XLSX 和 PPTX)編程接口,在此基礎(chǔ)上進(jìn)行了很多優(yōu)化、并實(shí)現(xiàn)DOCX、PPTX、Html等文件合并、拆分、互相轉(zhuǎn)換等實(shí)用的功能。


項(xiàng)目簡介

這是一個(gè)Open XML 文檔編程接口開發(fā)的,并擴(kuò)展了Open XML SDK的功能。

它支持以下功能:

1、將docx、pptx文件拆分為多個(gè)文件;

2、將多個(gè)docx、pptx文件合并為一個(gè)文件;

3、使用XML數(shù)據(jù)模板生成docx文件;

4、doxc文檔高保值轉(zhuǎn)換為Html頁面;

5、html頁面高保值轉(zhuǎn)換為docx文檔;

6、支持正則表達(dá)式搜索和替換 DOCX/PPTX 中的內(nèi)容;

7、支持docx、pptx文件,管理跟蹤修訂,包括檢測跟蹤修訂和接受跟蹤修訂;

8、更新 DOCX/PPTX 文件中的圖表,包括更新緩存數(shù)據(jù)以及嵌入的 XLSX;

9、對(duì)比兩個(gè)doxc文件,并生成帶有修訂跟蹤標(biāo)記的doxc文檔,并支持檢索修訂列表;

10、支持從doxc文檔檢索,包括使用樣式、層次結(jié)構(gòu)、使用的語言與字體;

11、與直接編寫標(biāo)記相比,使用簡單得多的代碼編寫XLSX文件,包括一種可以編寫數(shù)百萬行的XLSX文檔的流式方法。

12、支持從Excel提取數(shù)據(jù),包括內(nèi)容的格式。


技術(shù)架構(gòu)

1、平臺(tái):net45;net46;netstandard2.0 開發(fā)

2、開發(fā)工具:Visual Studio 2017


項(xiàng)目結(jié)構(gòu)

使用方法

該項(xiàng)目集成了各種功能的使用示例,下面挑幾個(gè)常用的分享:

Html轉(zhuǎn)Docx

public static void ConvertToHtml(string file, string outputDirectory)    {var fi = new FileInfo(file);        Console.WriteLine(fi.Name);byte[] byteArray = File.ReadAllBytes(fi.FullName);using (MemoryStream memoryStream = new MemoryStream())        {            memoryStream.Write(byteArray, 0, byteArray.Length);using (WordprocessingDocument wDoc = WordprocessingDocument.Open(memoryStream, true))            {var destFileName = new FileInfo(fi.Name.Replace(".docx", ".html"));if (outputDirectory != null && outputDirectory != string.Empty)                {                    DirectoryInfo di = new DirectoryInfo(outputDirectory);if (!di.Exists)                    {throw new OpenXmlPowerToolsException("Output directory does not exist");                    }                    destFileName = new FileInfo(Path.Combine(di.FullName, destFileName.Name));                }var imageDirectoryName = destFileName.FullName.Substring(0, destFileName.FullName.Length - 5) + "_files";int imageCounter = 0;
var pageTitle = fi.FullName;var part = wDoc.CoreFilePropertiesPart;if (part != null)                {                    pageTitle = (string) part.GetXDocument().Descendants(DC.title).FirstOrDefault() ?? fi.FullName;                }
// TODO: Determine max-width from size of content area.                HtmlConverterSettings settings = new HtmlConverterSettings()                {                    AdditionalCss = "body { margin: 1cm auto; max-width: 20cm; padding: 0; }",                    PageTitle = pageTitle,                    FabricateCssClasses = true,                    CssClassPrefix = "pt-",                    RestrictToSupportedLanguages = false,                    RestrictToSupportedNumberingFormats = false,                    ImageHandler = imageInfo =>                    {                        DirectoryInfo localDirInfo = new DirectoryInfo(imageDirectoryName);if (!localDirInfo.Exists)                            localDirInfo.create();                        ++imageCounter;string extension = imageInfo.ContentType.Split('/')[1].ToLower();                        ImageFormat imageFormat = null;if (extension == "png")                            imageFormat = ImageFormat.Png;else if (extension == "gif")                            imageFormat = ImageFormat.Gif;else if (extension == "bmp")                            imageFormat = ImageFormat.Bmp;else if (extension == "jpeg")                            imageFormat = ImageFormat.Jpeg;else if (extension == "tiff")                        {// Convert tiff to gif.                            extension = "gif";                            imageFormat = ImageFormat.Gif;                        }else if (extension == "x-wmf")                        {                            extension = "wmf";                            imageFormat = ImageFormat.Wmf;                        }
// If the image format isn't one that we expect, ignore it,// and don't return markup for the link.if (imageFormat == null)return null;
string imageFileName = imageDirectoryName + "/image" +                            imageCounter.ToString() + "." + extension;try                        {                            imageInfo.Bitmap.Save(imageFileName, imageFormat);                        }catch (System.Runtime.InteropServices.ExternalException)                        {return null;                        }string imageSource = localDirInfo.Name + "/image" +                            imageCounter.ToString() + "." + extension;
                       XElement img = new XElement(Xhtml.img,new XAttribute(NoNamespace.src, imageSource),                            imageInfo.ImgStyleAttribute,                            imageInfo.AltText != null ?new XAttribute(NoNamespace.alt, imageInfo.AltText) : null);return img;                    }                };                XElement htmlElement = HtmlConverter.ConvertToHtml(wDoc, settings);
// Produce HTML document with <!DOCTYPE html > declaration to tell the browser// we are using HTML5.var html = new XDocument(new XDocumentType("html", null, null, null),                    htmlElement);var htmlString = html.ToString(SaveOptions.DisableFormatting);                File.WriteAllText(destFileName.FullName, htmlString, Encoding.UTF8);            }        }    }


Docx、PPTX文檔合并

var n = DateTime.Now;var tempDi = new DirectoryInfo(string.Format("ExampleOutput-{0:00}-{1:00}-{2:00}-{3:00}{4:00}{5:00}", n.Year - 2000, n.Month, n.Day, n.Hour, n.Minute, n.Second));            tempDi.create();
var sourceDi = new DirectoryInfo("../../");foreach (var file in sourceDi.GetFiles("*.docx"))                File.Copy(file.FullName, Path.Combine(tempDi.FullName, file.Name));foreach (var file in sourceDi.GetFiles("*.pptx"))                File.Copy(file.FullName, Path.Combine(tempDi.FullName, file.Name));
var fileList = Directory.GetFiles(tempDi.FullName, "*.docx");foreach (var file in fileList)            {var fi = new FileInfo(file);                Console.WriteLine(fi.Name);var newFileName = "updated-" + fi.Name;var fi2 = new FileInfo(Path.Combine(tempDi.FullName, newFileName));                File.Copy(fi.FullName, fi2.FullName);
using (var wDoc = WordprocessingDocument.Open(fi2.FullName, true))                {var chart1Data = new ChartData                    {                        SeriesNames = new[] {"Car","Truck","Van","Bike","Boat",                        },                        CategoryDataType = ChartDataType.String,                        CategoryNames = new[] {"Q1","Q2","Q3","Q4",                        },                        Values = new double[][] {new double[] {100, 310, 220, 450,                        },new double[] {200, 300, 350, 411,                        },new double[] {80, 120, 140, 600,                        },new double[] {120, 100, 140, 400,                        },new double[] {200, 210, 210, 480,                        },                    },                    };                    Chartupdater.updateChart(wDoc, "Chart1", chart1Data);
var chart2Data = new ChartData                    {                        SeriesNames = new[] {"Series"                        },                        CategoryDataType = ChartDataType.String,                        CategoryNames = new[] {"Cars","Trucks","Vans","Boats",                        },                        Values = new double[][] {new double[] {320, 112, 64, 80,                        },                    },                    };                    Chartupdater.updateChart(wDoc, "Chart2", chart2Data);
var chart3Data = new ChartData                    {                        SeriesNames = new[] {"X1","X2","X3","X4","X5","X6",                        },                        CategoryDataType = ChartDataType.String,                        CategoryNames = new[] {"Y1","Y2","Y3","Y4","Y5","Y6",                        },                        Values = new double[][] {new double[] {      3.0,      2.1,       .7,      .7,      2.1,      3.0,      },new double[] {      3.0,      2.1,       .8,      .8,      2.1,      3.0,      },new double[] {      3.0,      2.4,      1.2,     1.2,      2.4,      3.0,      },new double[] {      3.0,      2.7,      1.7,     1.7,      2.7,      3.0,      },new double[] {      3.0,      2.9,      2.5,     2.5,      2.9,      3.0,      },new double[] {      3.0,      3.0,      3.0,     3.0,      3.0,      3.0,      },                    },                    };                    Chartupdater.updateChart(wDoc, "Chart3", chart3Data);
var chart4Data = new ChartData                    {                        SeriesNames = new[] {"Car","Truck","Van",                        },                        CategoryDataType = ChartDataType.DateTime,                        CategoryFormatCode = 14,                        CategoryNames = new[] {                            ToExcelInteger(new DateTime(2013, 9, 1)),                            ToExcelInteger(new DateTime(2013, 9, 2)),                            ToExcelInteger(new DateTime(2013, 9, 3)),                            ToExcelInteger(new DateTime(2013, 9, 4)),                            ToExcelInteger(new DateTime(2013, 9, 5)),                            ToExcelInteger(new DateTime(2013, 9, 6)),                            ToExcelInteger(new DateTime(2013, 9, 7)),                            ToExcelInteger(new DateTime(2013, 9, 8)),                            ToExcelInteger(new DateTime(2013, 9, 9)),                            ToExcelInteger(new DateTime(2013, 9, 10)),                            ToExcelInteger(new DateTime(2013, 9, 11)),                            ToExcelInteger(new DateTime(2013, 9, 12)),                            ToExcelInteger(new DateTime(2013, 9, 13)),                            ToExcelInteger(new DateTime(2013, 9, 14)),                            ToExcelInteger(new DateTime(2013, 9, 15)),                            ToExcelInteger(new DateTime(2013, 9, 16)),                            ToExcelInteger(new DateTime(2013, 9, 17)),                            ToExcelInteger(new DateTime(2013, 9, 18)),                            ToExcelInteger(new DateTime(2013, 9, 19)),                            ToExcelInteger(new DateTime(2013, 9, 20)),                        },                        Values = new double[][] {new double[] {1, 2, 3, 2, 3, 4, 5, 4, 5, 6, 5, 4, 5, 6, 7, 8, 7, 8, 8, 9,                        },new double[] {2, 3, 3, 4, 4, 5, 6, 7, 8, 7, 8, 9, 9, 9, 7, 8, 9, 9, 10, 11,                        },new double[] {2, 3, 3, 3, 3, 2, 2, 2, 3, 2, 3, 3, 4, 4, 4, 3, 4, 5, 5, 4,                        },                    },                    };                    Chartupdater.updateChart(wDoc, "Chart4", chart4Data);                }            }
           fileList = Directory.GetFiles(tempDi.FullName, "*.pptx");foreach (var file in fileList)            {var fi = new FileInfo(file);                Console.WriteLine(fi.Name);var newFileName = "updated-" + fi.Name;var fi2 = new FileInfo(Path.Combine(tempDi.FullName, newFileName));                File.Copy(fi.FullName, fi2.FullName);
using (var pDoc = PresentationDocument.Open(fi2.FullName, true))                {var chart1Data = new ChartData                    {                        SeriesNames = new[] {"Car","Truck","Van",                        },                        CategoryDataType = ChartDataType.String,                        CategoryNames = new[] {"Q1","Q2","Q3","Q4",                        },                        Values = new double[][] {new double[] {320, 310, 320, 330,                        },new double[] {201, 224, 230, 221,                        },new double[] {180, 200, 220, 230,                        },                    },                    };                    Chartupdater.updateChart(pDoc, 1, chart1Data);                }            }


執(zhí)行Excel公式

var n = DateTime.Now;var tempDi = new DirectoryInfo(string.Format("ExampleOutput-{0:00}-{1:00}-{2:00}-{3:00}{4:00}{5:00}", n.Year - 2000, n.Month, n.Day, n.Hour, n.Minute, n.Second));            tempDi.create();
// Change sheet name in formulasusing (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(                SmlDocument.fromFileName("../../Formulas.xlsx")))            {using (SpreadsheetDocument doc = streamDoc.GetSpreadsheetDocument())                {                    WorksheetAccessor.FormulaReplaceSheetName(doc, "Source", "'Source 2'");                }                streamDoc.GetModifiedSmlDocument().SaveAs(Path.Combine(tempDi.FullName, "Formulasupdated.xlsx"));            }
// Change sheet name in formulasusing (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(                SmlDocument.fromFileName("../../Formulas.xlsx")))            {using (SpreadsheetDocument doc = streamDoc.GetSpreadsheetDocument())                {                    WorksheetPart sheet = WorksheetAccessor.GetWorksheet(doc, "References");                    WorksheetAccessor.CopyCellRange(doc, sheet, 1, 1, 7, 5, 4, 8);                }                streamDoc.GetModifiedSmlDocument().SaveAs(Path.Combine(tempDi.FullName, "FormulasCopied.xlsx"));            }


具體示例代碼如下:


該文章在 2023/8/16 9:29:51 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場、車隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場作業(yè)而開發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲(chǔ)管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

黄频国产免费高清视频,久久不卡精品中文字幕一区,激情五月天AV电影在线观看,欧美国产韩国日本一区二区
天天在线精品一区 | 日本三级香港三级三级人!妇久 | 亚洲第一在线精品 | 午夜福利国产精品久久婷婷 | 欧美亚洲国产aⅴ人妖 | 中文有码亚洲视频精品一区 |