Microsoft Office是由Microsoft(微軟)公司開發的一套辦公軟件套裝。常用組件有 Word、Excel、PowerPoint等。Microsoft Office是一套由微軟公司開發的辦公軟件,它為 Microsoft Windows 和 Mac OS X而開發。 ?onenote是一款十分好用的筆記本工具,我想將onenote筆記本轉換成html并上傳到我的云服務器中,這樣在別的主機上就可以查看我做過的筆記。onenote本身并不支持筆記保存為html格式,不過無所不能的插件”GemforOnenote”有這個功能,只是一個個轉換過于麻煩,于是使用aspose.notefor.Net將onenote筆記本批量轉換成html。 aspose系列提供了word,pdf,excel等文件的操作接口,aspose.notefor.Net顧名思義就是用.Net庫對onenote文件進行操作。 安裝 在官網上下載aspose.notefor.Net,(下載鏈接戳這里),用visualstudio新建一個工程,在引用里添加下載的dll文件,就可以使用aspose.note了。 ![]()
按照我的理解,aspose.note是將一個onenote文件劃分成很多個層級,用一種類似于DOM的文檔結構模型來表述文件。一個頁面上的元素稱之為節點(Node),節點又分為CompositeNode和Node,區別在于前者可以包含其它Node,后者不能。頁面元素大多繼承自這兩個基類。 舉個例子,一個Document節點由很多個Page子節點構成,Page節點又由Title,Image,RichText等節點構成。每種節點都有相應的類,對其實例化后就可以進行具體的頁面元素操作。 ![]() ![]()
//Forcompleteexamplesanddatafiles,pleasegotohttps://github.com/kashifiqb/Aspose.Note-for-.NET //Thepathtothedocumentsdirectory. stringdataDir=RunExamples.GetDataDir_LoadingAndSaving(); //LoadthedocumentintoAspose.Note. DocumentoneFile=newDocument(dataDir+"Aspose.one"); dataDir=dataDir+"SaveWithDefaultSettings_out.pdf"; //SavethedocumentasPDF oneFile.Save(dataDir,SaveFormat.Pdf); ? Save()方法有兩個參數,第一個參數是路徑名稱,第二個參數可以是一個枚舉類型SaveFormat,指定保存類型,還可以是SaveOptions及其子類的實例化對象,SaveOptions對象可以在保存時進一步設置,例如從第二頁開始保存可以這么寫: //Forcompleteexamplesanddatafiles,pleasegotohttps://github.com/kashifiqb/Aspose.Note-for-.NET //Thepathtothedocumentsdirectory. stringdataDir=RunExamples.GetDataDir_LoadingAndSaving(); //LoadthedocumentintoAspose.Note. DocumentoneFile=newDocument(dataDir+"Aspose.one"); //InitializeImageSaveOptionsobject ImageSaveOptionsopts=newImageSaveOptions(SaveFormat.Png); //Setpageindex opts.PageIndex=1; dataDir=dataDir+"ConvertSpecificPageToImage_out.png"; //SavethedocumentasPNG. oneFile.Save(dataDir,opts);
以及,更多的使用方法見官網上的開發指南DevelopGuide 多個page遇到問題 在官網上給的例子都是單頁的.one文件,但在多頁的情況下(即,onenote右邊的“添加頁”那一欄有多個標簽),直接調用Save()方法保存得到的html只有第一個page的內容,而且是錯位的。官網上沒有說到多頁保存時應該怎么處理,但是既然保存時可以設置PageIndex和PageCount,那么應該是支持多頁保存的。我想可能是因為我的aspose.note不兼容我的onenote版本。 首先想到,是不是可以對于每一個Page,都創建一個空白的Document對象,然后把每個Page節點AppendChild()到這個Document對象上,再把這個Document保存成html。然而報錯thenodebelongstotheothernode。這是因為節點之間有嚴格的從屬關系,不能隨便Append,(我們創建對象的時候就可以看到,例如Pagepage=newPage(doc),這個doc就是一個Document對象,創建對象時就確立了從屬關系)即便將之前的Document對象調用RemoveChild()刪除Page節點仍然不能讓這個節點附著到另一個Document對象上。 那么只好另辟蹊徑了,可以用GetChildNodes()得到一個Page的列表,遍歷這個列表,每次保存一個Page。 IList intcount=pages.Count(); for(inti=0;i<> Pagepage=pages[i]; HtmlSaveOptionshs=newHtmlSaveOptions(); hs.PageIndex=i; hs.PageCount=1; Console.WriteLine("正在創建HTML"+Path.Combine(child_child_dir,page.Title.TitleText.Text+".html...")); doc.Save(Path.Combine(child_child_dir,page.Title.TitleText.Text+".html"),hs); Console.WriteLine("完成"); } ? 對比文件修改時間,只更新修改過的.one文件 可以稍稍改進一下代碼。我們不必每次都生成所有html,可以只有更新后,即當page的修改時間比對應的html文件的修改時間還要遲時才生成。改進后的代碼如下: IList intcount=pages.Count(); for(inti=0;i<> Pagepage=pages[i]; //若無對應的htmnl文件,或有且源文件(page)修改時間大于目標文件(html),則生成html stringdes_path=Path.Combine(child_child_dir,page.Title.TitleText.Text+".html"); FileInfodes_file=newFileInfo(des_path); if(File.Exists(des_path)){ DateTimedes_time=des_file.LastWriteTime; DateTimesrc_time=page.LastModifiedTime; if(DateTime.Compare(src_time,des_time)<> continue; } HtmlSaveOptionshs=newHtmlSaveOptions(); hs.PageIndex=i; hs.PageCount=1; Console.WriteLine("正在創建HTML"+des_path+"..."); doc.Save(des_path,hs); Console.WriteLine("完成"); } ? 完整代碼及運行截圖 usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; usingAspose.Note; usingAspose.Note.Saving; usingSystem.IO; namespaceOnenote_Up { classProgram { staticvoidMain(string[]args) { stringroot_dir=@"C:\Users\fiver\Desktop\onenote\my_onenote\onenote"; stringnew_root_dir=@"C:\Users\fiver\Desktop\onenote\my_onenote\html"; DirectoryInfoRootDir=newDirectoryInfo(root_dir); DirectoryInfoNewRootDir=newDirectoryInfo(new_root_dir); foreach(DirectoryInfopart_dirinRootDir.GetDirectories()){ stringchild_dir=Path.Combine(root_dir,part_dir.Name); stringnew_child_dir=Path.Combine(new_root_dir,part_dir.Name); DirectoryInfoChildDir=newDirectoryInfo(child_dir); //若沒有文件夾則創建 if(!Directory.Exists(child_dir)) { Directory.CreateDirectory(new_child_dir); } //遍歷每個文件夾的每個one文件,一個one文件生成對應的一個文件夾,每個one文件的多個page生成多個html放在相應的文件夾下面 else { foreach(FileInfonoteinChildDir.GetFiles()){ //只有后綴為one的文件才處理 if(note.Name.Substring(note.Name.Length-3,3)=="one") { Documentdoc=newDocument(note.FullName); //創建對應的文件夾 stringchild_child_dir=Path.Combine(new_child_dir,note.Name.Substring(0,note.Name.Length-4)); if(!Directory.Exists(child_child_dir)) { Console.WriteLine("創建文件夾"+child_child_dir); Directory.CreateDirectory(child_child_dir); } Console.WriteLine("進入文件夾"+child_child_dir); //獲得.one文件的全部page并遍歷 //IsComposite:Checkswhetherthenodeiscomposite.Iftruethenthenodecanhavechildnodes.只有一個page也為true? if(doc.IsComposite){ IList intcount=pages.Count(); for(inti=0;i<> Pagepage=pages[i]; //若無對應的htmnl文件,或有且源文件(page)修改時間大于目標文件(html),則生成html stringdes_path=Path.Combine(child_child_dir,page.Title.TitleText.Text+".html"); FileInfodes_file=newFileInfo(des_path); if(File.Exists(des_path)){ DateTimedes_time=des_file.LastWriteTime; DateTimesrc_time=page.LastModifiedTime; Console.WriteLine(des_path); Console.WriteLine(src_time.ToString()); Console.WriteLine(des_time.ToString()); if(DateTime.Compare(src_time,des_time)<> continue; } HtmlSaveOptionshs=newHtmlSaveOptions(); hs.PageIndex=i; hs.PageCount=1; Console.WriteLine("正在創建HTML"+des_path+"..."); //doc.Save(des_path,hs); Console.WriteLine("完成"); } } } } } } Console.WriteLine("Finished.Pressanykeytoexit...."); Console.ReadKey(); } } } ? ? Office辦公軟件是辦公的第一選擇,這個地球人都知道。Microsoft Office 2010的新界面簡潔明快,標識也改為了全橙色。 |
溫馨提示:喜歡本站的話,請收藏一下本站!