我想使用C#Interop读取Word文档并将其填充到DataGrid中 我尝试通过c#中的简单控制台程序阅读Word文档。
但我是Asp.net的新手,想知道如何实施该计划 我可以创建Word文档,但希望将其填充到DATA网格。
string filePath = null;
OpenFileDialog file = new OpenFileDialog();
file.Title = "Word File";
file.InitialDirectory = "C:\\";
file.RestoreDirectory = true;
// execute if block when dialog result box click ok button
if (file.ShowDialog() == DialogResult.OK) {
// store selected file path
filePath = file.FileName.ToString();
}
try
{
// create word application
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.ApplicationClass();
// create object of missing value
object miss = System.Reflection.Missing.Value;
// create object of selected file path
object path = filePath;
// set file path mode
object readOnly = false;
// open document
Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss,ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
docs.ActiveWindow.Selection.WholeStory();
docs.ActiveWindow.Selection.Copy();
IDataObject data = Clipboard.GetDataObject();
//populate data into Datagridview?
docs.Close(ref miss, ref miss, ref miss);
答案 0 :(得分:0)
我建议您使用Apache NPOI,它有大量的库来读取MS Word和其他MS Office文档。
对于JAVA答案 1 :(得分:0)
访问这些链接我想你会在那里找到答案。
编辑:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Read and Display Data From an Word File (.doc or .docx) in ASP.NET</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<b>Please Select Excel File: </b>
<asp:FileUpload ID="fileuploadExcel" runat="server" />
<asp:Button ID="btnImport" runat="server" Text="Import Data" OnClick="btnImport_Click" />
<br />
<asp:Label ID="lblMessage" runat="server" Visible="False" Font-Bold="True" ForeColor="#009933"></asp:Label><br />
<asp:GridView ID="grvWordData" runat="server">
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>
现在打开文件后面的代码并添加以下命名空间
using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
之后在
后面的代码中编写以下代码C#.NET代码
protected void btnImport_Click(object sender, EventArgs e) {
string connString = "";
string strFileType = Path.GetExtension(fileuploadWord.FileName).ToLower();
string path = fileuploadWord.PostedFile.FileName;
//Connection String to Word file
if (strFileType.Trim() == ".doc") {
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Word 8.0;HDR=Yes;IMEX=2\"";
}
else if (strFileType.Trim() == ".docx") {
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Word 12.0;HDR=Yes;IMEX=2\"";
}
string query = "SELECT * FROM [document1$]";
OleDbConnection conn = new OleDbConnection(connString);
if (conn.State == ConnectionState.Closed)
conn.Open();
OleDbCommand cmd = new OleDbCommand(query, conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
grvWordData.DataSource = ds.Tables[0];
grvWordData.DataBind();
da.Dispose();
conn.Close();
conn.Dispose();
}