简体   繁体   English

如何将Openoffice Calc Excel连接到SQL Server而不是ASP.NET中的oledb Excel连接

[英]How to take Openoffice calc excel connection to sql server instead of oledb excel connection in asp.net

In my web application i need to import OpenOffice calc data into SQL server database i searched in google but i didn't get i found one ole-db connection instead of that how can i take open office calc data connection. 在我的Web应用程序中,我需要将OpenOffice calc数据导入到我在Google中搜索到的SQL Server数据库中,但是我没有得到一个ole-db连接,而是该如何进行Open Office calc数据连接。

This is the ole-db connection: 这是ole-db连接:

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

and how can i get open office calc to connection. 以及如何获取连接的开放式办公软件。

Updated Code: 更新的代码:

   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();
}

can anyone tell to me how can i do this 谁能告诉我该怎么做

Thank you 谢谢

My best excell convertion tool is LinqToExcel 我最好的excell转换工具是LinqToExcel

Just create a Model say 只需创建一个模型说

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

and One line of code 和一行代码

   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();
            }

where ExcelFullData is database entity Just show the file location of excel in webServer ...not sure about Openoffice give it a try :) ExcelFullData是数据库实体的地方,只是显示excel在webServer中的文件位置...不确定Openoffice ,请尝试一下:)

For oledb 对于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);
                }
            }

and database connection 和数据库连接

    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();
                        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM