我需要從我的專案中的檔案夾中獲取 .docx 檔案,并通過在 asp.net 視窗形式中使用 gridview 將其顯示給用戶。
uj5u.com熱心網友回復:
好的,困難的部分是從word檔案中取出表格。
你沒有提及或分享你是如何做到這一點的。
你注意到一個“.doc”檔案與一個“.docx”檔案。這是一個巨大的問題,因為 .doc 檔案是二進制格式的 word 檔案,而 .docx 是一種基于開放 (xml) 的格式,其中 word 檔案中的文本和標記實際上是 xml,并且標記為純文本。事實上,.docx 檔案可以重命名為 zip 檔案,您可以打開它(嘗試重命名 docx 檔案)。
但是,這確實假設您的 Web 服務器將安裝 word。這種型別的場景不被真正支持,因為 word (大部分)是非托管代碼,更糟糕的是它不被認為是執行緒安全的。結果,您不能以這種方式真正“自動化”單詞并具有高度的可靠性。
下一個問題當然是大多數(如果不是全部)Web 服務器都以 x64 位運行,并且我們經常發現 word office 安裝是 x32 位。當使用我們所說的非 .net 代碼(在本例中為 applcation)時,您不能如前所述混淆位大小。因此,要么將網站作為 x32 位運行(不再很常見),要么確實確保安裝了 word x64 位)。
因此,雖然這不是很多代碼,但這里有大量的移動部件,因此任何型別的用戶或重負載下的此類代碼都不會產生可靠性或穩健的設定。您可能正在使用一些 xml 和 3rd 方 word 檔案庫,但不建議在網站上使用 office inter-op。
好的,所以現在我們這里有所有的問題?
所以,讓我們用 .net 的 office 互操作功能來做這件事。同樣:這種型別的設定不是執行緒安全的,不推薦用于網站。事實上,如今可用的虛擬主機中很少(實際上沒有)會安裝 word office。也許您運行自己的服務器,并且您可以并且可以免費在該服務器上安裝 Office,但請記住這個非常大的挑戰和問題(您可以假設 Office 和 Word 將安裝在 Web 服務器上的可能性很小, 而且使用 office inter-op 不是執行緒安全的(這意味著網站上有很多用戶時這根本無法很好地擴展)。
好的,那么,我們找一個帶表格的word檔案,打開它?
好的,我有這個:

那么,讀取該表并將其顯示在網格視圖中的 .net 代碼會是什么樣子?
好吧,如前所述,我們為此使用 .net 官員互操作:
So, we need a to set a reference to Microsoft.Office.Interop.Word
And our mark up on the page can look like this (a simple button to click to run our code, and a grid view)
<div style="width:50%;padding:25px">
<br />
<asp:Button ID="Button1" runat="server" Text="Show word table"
CssClass="btn" OnClick="Button1_Click"/>
<br />
<br />
<br />
<asp:GridView ID="GridView1" runat="server" CssClass="table"></asp:GridView>
</div>
and our code is thus this:
We open the word doc, get the table, and then create a .net data table.
Fill the table.
Then send that .net table to the grid view
protected void Button1_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Word.Application MyWord = new Microsoft.Office.Interop.Word.Application();
string sFile = @"c:\Test7\Fighter.docx";
// create a .net table
DataTable rstData = new DataTable();
MyWord.Documents.Open(sFile);
Microsoft.Office.Interop.Word.Document MyDoc = MyWord.ActiveDocument;
// get first table
Microsoft.Office.Interop.Word.Table MyTable = MyDoc.Tables[1];
// assume first row is headings
int iCol = 0;
for (iCol = 1; iCol <= MyTable.Columns.Count; iCol )
{
rstData.Columns.Add(CleanCellText(MyTable.Rows[1].Cells[iCol].Range.Text), typeof(string));
}
// now add all the rows of data to the .net table
int iRows = 0;
// word tables start at 1, .net tables start at 0 (zero based)
for (iRows = 2; iRows <= MyTable.Rows.Count; iRows )
{
DataRow OneRow = rstData.NewRow();
for (iCol = 1; iCol <= MyTable.Columns.Count; iCol )
{
OneRow[iCol - 1] = CleanCellText(MyTable.Rows[iRows].Cells[iCol].Range.Text);
}
rstData.Rows.Add(OneRow); // add this row to .net table
}
MyDoc.Close();
MyWord.Quit();
// now bind the resulting table to the grid
GridView1.DataSource = rstData;
GridView1.DataBind();
}
string CleanCellText(string s)
{
// all word cells have a training cr and a char(7)
string MyClean = ((char)13).ToString() ((char)7).ToString();
return s.Replace(MyClean,"");
}
running above, we get this:

Note the CleanCellText() little function. It turns out that all word tables text is followed by a carriage return, and then a char(7). So, you can remove them by shortening the cell text by 2 chars, or do a replace as per that helper function.
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/435136.html
