English 中文(简体)
我们是否需要在服务器上安装微软办公室, 用于 Asp. net Excel 导入?
原标题:Do we need to install Microsoft office in server for Excel import in Asp.net?

我们是否需要在服务器上安装 Microsoft 办公室, 以运行将数据从优异文件导入到 mssql 数据库的程序?

任何建议或想法?

i 使用的代码

public partial class _Default : System.Web.UI.Page
{
private String strConnection = "Data Source=MYCBJ017550027;Initial Catalog=MySamplesDB;Integrated Security=True";
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSend_Click(object sender, EventArgs e)
{
string path = fileuploadExcel.PostedFile.FileName;
string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;Persist Security Info=False";
OleDbConnection excelConnection =new OleDbConnection(excelConnectionString);
OleDbCommand cmd = new OleDbCommand("Select [ID],[Name],[Designation] from [Sheet1$]",excelConnection);
excelConnection.Open();
OleDbDataReader dReader;
dReader = cmd.ExecuteReader();
SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);
sqlBulk.DestinationTableName = "Excel_table";
sqlBulk.WriteToServer(dReader);
excelConnection.Close();
}
}
最佳回答

如果您只读取 xls 文件, 请使用您的. net 框架所嵌入的 < code> microsoft. Jet. OLEDB. 4. 0

如果您正在读取 xlsx 文件, 请使用 microsoft. ACE. OLEDB.12. 0 。 驱动程序可从 Microsoft 网站自由下载。 您不需要安装 Microsoft 官员来连接 。

在连接字符串后使用

    string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;  
Data Source=" + path + ";Extended Properties=Excel 12.0;HDR=YES";

< a href=>""http://www.microsoft.com/en-us/ download/ details.aspx?id=13255" rel="noreferr">从这里下降的驾驶员

请参考此实例

问题回答

@Romil表示你可以使用.NET框架:

string ConnectionString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}; Extended Properties=""Excel 8.0;HDR=Yes"";", fileName);

using (OleDbConnection conn = new OleDbConnection(ConnectionString))
{
     conn.Open();
     DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });

     foreach (DataRow schemaRow in schemaTable.Rows)
     {
          string sheet = schemaRow["TABLE_NAME"].ToString();
          using (OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + sheet + "]", conn))
          {
               cmd.CommandType = CommandType.Text;
               DataTable outputTable = new DataTable(sheet);
               output.Tables.Add(outputTable);
               new OleDbDataAdapter(cmd).Fill(outputTable);
          }
     }
                conn.Close();
}

喷气机的问题在于, 您仍然需要安装数据提供者来操作它( 虽然它也是免费的), 或者为它安装办公室来操作它。 如果您只需要阅读一个优秀的文件, 外面有很多管理完善的 libs, 不需要安装任何设备就能完成轨道 。

我在这里列举了几个,我使用过很多优秀的阅读器,并取得了良好的成果。

http://excelreader.codeplex.com/
http://epplus.codeplex.com/

Excel 阅读器似乎在文件方面很浅。 因此,这里的例子说明如何使用它

IExcelDataReader reader = null;
try
{
    using (FileStream stream = new FileStream(ofd.FileName, FileMode.Open))
    {
        string ext = System.IO.Path.GetExtension(ofd.FileName).Replace(".", "").ToUpper();
        if (ext == "XLS")
            reader = ExcelReaderFactory.CreateBinaryReader(stream);
        else
            reader = ExcelReaderFactory.CreateOpenXmlReader(stream);

        reader.IsFirstRowAsColumnNames = true;
        using (DataSet ds = reader.AsDataSet())
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                ImportData toAdd = new ImportData()
                    {
                        Format = dr[0].ToString(),
                    };

                Database.Datastore.InsertObject(toAdd);
            }
        }
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签