【问题标题】:How to take Openoffice calc excel connection to sql server instead of oledb excel connection in asp.net如何将 Openoffice calc excel 连接到 sql server 而不是 asp.net 中的 oledb excel 连接
【发布时间】:2016-09-28 05:48:44
【问题描述】:

在我的 Web 应用程序中,我需要将 OpenOffice calc 数据导入到我在 google 中搜索的 SQL Server 数据库中,但我没有找到一个 ole-db 连接,而不是那个,我该如何使用 Open Office calc 数据连接。

这是 ole-db 连接:

 string excelConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", path);

我怎样才能获得开放的办公室计算来连接。

更新代码:

   using System.Text;
    using unoidl.com.sun.star.uno;
    using unoidl.com.sun.star.lang;
    using unoidl.com.sun.star.frame;
    using unoidl.com.sun.star.beans;
    using unoidl.com.sun.star.sheet;
    using unoidl.com.sun.star.container;
    using unoidl.com.sun.star.table;
    using unoidl.com.sun.star.text;
 protected void import_Click(object sender, EventArgs e)
   {
      XComponentContext oStrap = uno.util.Bootstrap.bootstrap();
                XMultiServiceFactory oServMan = (XMultiServiceFactory)oStrap.getServiceManager();
                XComponentLoader desktop = (XComponentLoader)oServMan.createInstance("com.sun.star.frame.Desktop");
                string url = @"private:factory/scalc";  
                PropertyValue[] loadProps = new PropertyValue[1];
                XComponent document = desktop.loadComponentFromURL(url, "_blank", 0, loadProps);
                XSpreadsheets oSheets = ((XSpreadsheetDocument)document).getSheets();
                XIndexAccess oSheetsIA = (XIndexAccess)oSheets;
                XSpreadsheet sheet = (XSpreadsheet)oSheetsIA.getByIndex(0).Value;
                current = ((XText)sheet.getCellByPosition(0, iRow)).getString();
}

谁能告诉我该怎么做

谢谢

【问题讨论】:

  • 我已经在我的问题网络应用程序中提到过
  • 一件事...我没有安装 Open Office ..所以上传一个示例 .xls 文件
  • 我的工作表也是 .xlsx 格式,但我没有 ms-office 我需要打开 .xlsx 文件,因为我使用的是 Open Office 或 Libra Office。
  • 将文件分享给我们,以便我们在本地机器上试用
  • 可以创建一个.xlsx格式的文件

标签: c# asp.net sql-server openoffice-calc


【解决方案1】:

对于 oledb

           //Read excell
            string input_Excell_fileName = @"Your Path";
            var connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + input_Excell_fileName + ";Extended Properties=\"Excel 12.0;IMEX=1;HDR=NO;TypeGuessRows=0;ImportMixedTypes=Text\""; ;
            DataSet ds;
            using (var conn = new OleDbConnection(connectionString))
            {
                conn.Open();
                var sheets = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT * FROM [" + sheets.Rows[0]["TABLE_NAME"].ToString() + "] ";

                    var adapter = new OleDbDataAdapter(cmd);
                    ds = new DataSet();
                    adapter.Fill(ds);
                }
            }

和数据库连接

    string WSMSconnectionstring = @"Data Source=MHAMUDUL\WALTON;Initial Catalog=WSMS;Integrated Security=True";
//Or
   // string WSMSconnectionstring = @"Data Source=MHAMUDUL\WALTON;Initial Catalog=WSMS;Persist Security Info=True;User ID=sa;Password=ssssss";
     using (SqlConnection con = new SqlConnection(WSMSconnectionstring))
                         {
                            con.Open();
                            string query1 = String.Format(@" Select * from ServiceTimeLog where ServiceID={0} and QCReleaseStatus is  null
     ", ServiceID);
                            SqlCommand cmd1 = new SqlCommand(query1, con);
                            SqlDataReader reader = cmd1.ExecuteReader();
                            if (reader.HasRows)
                            {
                                reader.Dispose();
                                //Update
                                string update = String.Format(@" update  ServiceTimeLog set ServiceDate=GETDATE(), StartTime=GETDATE(),EndTime=GETDATE(),QCStartTime=GETDATE(),QCEndTime=GETDATE(),QCReleaseStatus='QCPassed',ReleaseStatus='SendToQC',TechnicianID=(select UserID from Users where UserName='{0}'),QCID=(select UserID from Users where UserName='{1}') where ServiceID={2}", TechnicainID, QCID, ServiceID);
                                SqlCommand updateCmd = new SqlCommand(update, con);
                                updateCmd.ExecuteNonQuery();

                            }
                            else
                            {
                                reader.Dispose();
                                //Insert
                                string insert = String.Format(@"insert into ServiceTimeLog(ServiceID,ReleaseStatus,ServiceDate,StartTime,EndTime,QCStartTime,QCEndTime,QCReleaseStatus,TechnicianID,QCID)  values({0},'SendToQC',GETDATE(),GETDATE(),GETDATE(),GETDATE(),GETDATE(),'QCPassed',(select UserID from Users where UserName='{1}'),(select UserID from Users where UserName='{2}'))", ServiceID, TechnicainID, QCID);
                                SqlCommand insertCmd = new SqlCommand(insert, con);
                                insertCmd.ExecuteNonQuery();
                            }
                            cmd1.Dispose();
                            reader.Dispose();
                        }

【讨论】:

    【解决方案2】:

    我最好的excel转换工具是LinqToExcel

    只需创建一个模型说

    public class ExcelData
        {
            [ExcelColumn("IMEI1")]
            public String IMEI1 { get; set; }
            [ExcelColumn("IMEI2")]
            public String IMEI2 { get; set; }
            [ExcelColumn("COLOR")]
            public String COLOR { get; set; }
        }
    

    还有一行代码

       var excel = new ExcelQueryFactory();
                    List<ExcelData> allListData = new List<ExcelData>();
                    try
                    {
                        excel = new LinqToExcel.ExcelQueryFactory(@"C:\Users\Mhamudul Hasan\Desktop\FulllData.xlsx");
                        allListData = (from c in excel.Worksheet<ExcelData>("Sheet1")
    
                                       select c).ToList();
                    }
                    catch (Exception exception)
                    {
    
    
                        return;
                    }
    List<ExcelFullData> entities = new List<ExcelFullData>();
                using (var ctx = new testEntities())
                {
    
                    foreach (var data in allListData)
                    {
                        ExcelFullData excel1 = new ExcelFullData
                        {
                            IMEI1 = data.IMEI1,
                            IMEI2 = data.IMEI2,
                            Color = data.COLOR
                        };
                        //entities.Add(excel1);
                        ctx.ExcelFullDatas.Add(excel1);
    
                    }
                    ctx.SaveChanges();
                }
    

    其中 ExcelFullData 是数据库实体 只需在 webServer 中显示 excel 的文件位置...不确定Openoffice 试试看:)

    【讨论】:

    • 感谢您的回复,是不是只有 oledb 连接而不是连接我需要重新编写所有导入到 sql server 连接的内容,请告诉我
    • 我也在我的几个项目中尝试过 oledb 连接..但未知原因..每次都不起作用...所以我提供了另一种方法
    • 我尝试插入您的代码,但在 ExcelQueryFactory()、 和 LinqToExcel 上出现错误。我如何在我的系统中执行此操作我没有 ms-office 我只有 Open Office 和 Libra Office,而且我的工作表是 .xlsx 或 .xls 格式,你能帮帮我吗?谢谢
    • 不需要...只需安装nuget包nuget.org/packages/LinqToExcel ..我会工作
    • 没有 ms-office 时我使用 oledb 连接是否可以正常工作
    猜你喜欢
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多