使用NOPI讀取Excel的例子很多,讀取Word的例子不多。
成都一家集口碑和實力的網(wǎng)站建設服務商,擁有專業(yè)的企業(yè)建站團隊和靠譜的建站技術,十載企業(yè)及個人網(wǎng)站建設經驗 ,為成都上千多家客戶提供網(wǎng)頁設計制作,網(wǎng)站開發(fā),企業(yè)網(wǎng)站制作建設等服務,包括成都營銷型網(wǎng)站建設,成都品牌網(wǎng)站建設,同時也為不同行業(yè)的客戶提供成都網(wǎng)站建設、成都網(wǎng)站設計的服務,包括成都電商型網(wǎng)站制作建設,裝修行業(yè)網(wǎng)站制作建設,傳統(tǒng)機械行業(yè)網(wǎng)站建設,傳統(tǒng)農業(yè)行業(yè)網(wǎng)站制作建設。在成都做網(wǎng)站,選網(wǎng)站制作建設服務商就選創(chuàng)新互聯(lián)。Excel的解析方式有多中,可以使用ODBC查詢,把Excel作為一個數(shù)據(jù)集對待。也可以使用文檔結構模型的方式進行解析,即解析Workbook(工作簿)、Sheet、Row、Column。
Word的解析比較復雜,因為Word的文檔結構模型定義較為復雜。解析Word或者Excel,關鍵是理解Word、Excel的文檔對象模型。
Word、Excel文檔對象模型的解析,可以通過COM接口調用,此類方式使用較廣。(可以錄制宏代碼,然后替換為對應的語言)
也可以使用XML模型解析,尤其是對于2007、2010版本的文檔的解析。
using NPOI.POIFS.FileSystem; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using NPOI.XWPF.UserModel; using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Text; namespace eyuan { public static class NOPIHandler { ////// /// /// ///public static List >> ReadExcel(string fileName) { //打開Excel工作簿 XSSFWorkbook hssfworkbook = null; try { using (FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { hssfworkbook = new XSSFWorkbook(file); } } catch (Exception e) { LogHandler.LogWrite(string.Format("文件{0}打開失敗,錯誤:{1}", new string[] { fileName, e.ToString() })); } //循環(huán)Sheet頁 int sheetsCount = hssfworkbook.NumberOfSheets; List
>> workBookContent = new List
>>(); for (int i = 0; i < sheetsCount; i++) { //Sheet索引從0開始 ISheet sheet = hssfworkbook.GetSheetAt(i); //循環(huán)行 List
> sheetContent = new List
>(); int rowCount = sheet.PhysicalNumberOfRows; for (int j = 0; j < rowCount; j++) { //Row(邏輯行)的索引從0開始 IRow row = sheet.GetRow(j); //循環(huán)列(各行的列數(shù)可能不同) List
rowContent = new List (); int cellCount = row.PhysicalNumberOfCells; for (int k = 0; k < cellCount; k++) { //ICell cell = row.GetCell(k); ICell cell = row.Cells[k]; if (cell == null) { rowContent.Add("NIL"); } else { rowContent.Add(cell.ToString()); //rowContent.Add(cell.StringCellValue); } } //添加行到集合中 sheetContent.Add(rowContent); } //添加Sheet到集合中 workBookContent.Add(sheetContent); } return workBookContent; } /// /// /// /// ///public static string ReadExcelText(string fileName) { string ExcelCellSeparator = ConfigurationManager.AppSettings["ExcelCellSeparator"]; string ExcelRowSeparator = ConfigurationManager.AppSettings["ExcelRowSeparator"]; string ExcelSheetSeparator = ConfigurationManager.AppSettings["ExcelSheetSeparator"]; // List >> excelContent = ReadExcel(fileName); string fileText = string.Empty; StringBuilder sbFileText = new StringBuilder(); //循環(huán)處理WorkBook中的各Sheet頁 List
>>.Enumerator enumeratorWorkBook = excelContent.GetEnumerator(); while (enumeratorWorkBook.MoveNext()) { //循環(huán)處理當期Sheet頁中的各行 List
>.Enumerator enumeratorSheet = enumeratorWorkBook.Current.GetEnumerator(); while (enumeratorSheet.MoveNext()) { string[] rowContent = enumeratorSheet.Current.ToArray(); sbFileText.Append(string.Join(ExcelCellSeparator, rowContent)); sbFileText.Append(ExcelRowSeparator); } sbFileText.Append(ExcelSheetSeparator); } // fileText = sbFileText.ToString(); return fileText; } ///
/// 讀取Word內容 /// /// ///public static string ReadWordText(string fileName) { string WordTableCellSeparator = ConfigurationManager.AppSettings["WordTableCellSeparator"]; string WordTableRowSeparator = ConfigurationManager.AppSettings["WordTableRowSeparator"]; string WordTableSeparator = ConfigurationManager.AppSettings["WordTableSeparator"]; // string CaptureWordHeader = ConfigurationManager.AppSettings["CaptureWordHeader"]; string CaptureWordFooter = ConfigurationManager.AppSettings["CaptureWordFooter"]; string CaptureWordTable = ConfigurationManager.AppSettings["CaptureWordTable"]; string CaptureWordImage = ConfigurationManager.AppSettings["CaptureWordImage"]; // string CaptureWordImageFileName = ConfigurationManager.AppSettings["CaptureWordImageFileName"]; // string fileText = string.Empty; StringBuilder sbFileText = new StringBuilder(); #region 打開文檔 XWPFDocument document = null; try { using (FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { document = new XWPFDocument(file); } } catch (Exception e) { LogHandler.LogWrite(string.Format("文件{0}打開失敗,錯誤:{1}", new string[] { fileName, e.ToString() })); } #endregion #region 頁眉、頁腳 //頁眉 if (CaptureWordHeader == "true") { sbFileText.AppendLine("Capture Header Begin"); foreach (XWPFHeader xwpfHeader in document.HeaderList) { sbFileText.AppendLine(string.Format("{0}", new string[] { xwpfHeader.Text })); } sbFileText.AppendLine("Capture Header End"); } //頁腳 if (CaptureWordFooter == "true") { sbFileText.AppendLine("Capture Footer Begin"); foreach (XWPFFooter xwpfFooter in document.FooterList) { sbFileText.AppendLine(string.Format("{0}", new string[] { xwpfFooter.Text })); } sbFileText.AppendLine("Capture Footer End"); } #endregion #region 表格 if (CaptureWordTable == "true") { sbFileText.AppendLine("Capture Table Begin"); foreach (XWPFTable table in document.Tables) { //循環(huán)表格行 foreach (XWPFTableRow row in table.Rows) { foreach (XWPFTableCell cell in row.GetTableCells()) { sbFileText.Append(cell.GetText()); // sbFileText.Append(WordTableCellSeparator); } sbFileText.Append(WordTableRowSeparator); } sbFileText.Append(WordTableSeparator); } sbFileText.AppendLine("Capture Table End"); } #endregion #region 圖片 if (CaptureWordImage == "true") { sbFileText.AppendLine("Capture Image Begin"); foreach (XWPFPictureData pictureData in document.AllPictures) { string picExtName = pictureData.suggestFileExtension(); string picFileName = pictureData.GetFileName(); byte[] picFileContent = pictureData.GetData(); // string picTempName = string.Format(CaptureWordImageFileName, new string[] { Guid.NewGuid().ToString() + "_" + picFileName + "." + picExtName }); // using (FileStream fs = new FileStream(picTempName, FileMode.Create, FileAccess.Write)) { fs.Write(picFileContent, 0, picFileContent.Length); fs.Close(); } // sbFileText.AppendLine(picTempName); } sbFileText.AppendLine("Capture Image End"); } #endregion //正文段落 sbFileText.AppendLine("Capture Paragraph Begin"); foreach (XWPFParagraph paragraph in document.Paragraphs) { sbFileText.AppendLine(paragraph.ParagraphText); } sbFileText.AppendLine("Capture Paragraph End"); // // fileText = sbFileText.ToString(); return fileText; } } }
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。