使用oledb在同一个excel表上写多个表

时间:2018-02-09 12:54:54

标签: c# excel oledb

我正在创建带有xmlwriter的excel文件,由于xml属性和文件类型电子表格,它变得太大了250mb左右的文件。 我无法使用interop,因为用户不想在服务器上设置办公室 或第三部分dll。 现在我想,我应该使用Oledb来创建excel文件。 我的问题是我不能用oledb在同一张纸上写多个表。

错误:

  

标准表达式中的数据类型不匹配

我的代码:

           connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1}'";
           connectionString = string.Format(connectionString, outputFile, "YES");

            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                conn.Open();


                foreach (var item in dataSetList)
                {
                    DataSet data = (DataSet)item.Value;
                    // creates the excel sheet



                    //DataSet data = item.Keys
                    //Iterate the tables in the dataset.
                    //Each table will become a tab or sheet in the workbook.
                    int maxcount = 0;
                    for (int kl = data.Tables.Count-1; kl >=0; kl--)
                    {
                        DataTable dt = data.Tables[kl];
                        string sheetSql1 = "";
                        if (dt.Columns.Count > maxcount)
                            maxcount = dt.Columns.Count;

                        var tableSchemaName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                        if (tableSchemaName.Select("TABLE_NAME='" + data.DataSetName + "$'").Length == 0)
                        {
                            sheetSql1 = "CREATE TABLE [" + data.DataSetName + "] (";
                            if (dt.Columns.Count > 0)
                            {
                                //if (!dt.Columns[0].ColumnName.StartsWith("somespecificcaption"))
                                //{
                                for (int i = 0; i < dt.Columns.Count; i++)
                                {
                                    string dataType;


                                    //Set appropriate styling of each cell based on datatype
                                    //This depends on how sql server ends up reporting the datatype.
                                    switch (dt.Columns[i].DataType.ToString()) 
                                    {
                                        case "System.DateTime":

                                            dataType = "DateTime";

                                            break;
                                        case "System.Decimal":
                                        case "System.Double":
                                        case "System.Int16":
                                        case "System.Int32":
                                        case "System.Int64":
                                        case "System.Byte":
                                            dataType = "int";

                                            break;
                                        default:
                                            dataType = "varchar(100)";

                                            break;
                                    }
                                    sheetSql1 += "F" + i.ToString() + " " + dataType + ",";
                                    //sheetSql1 += dt.Columns[i].ColumnName + " varchar(50),";

                                }
                                sheetSql1 = sheetSql1.Remove(sheetSql1.Length - 1);
                                sheetSql1 += ")";
                                using (OleDbCommand cmd = new OleDbCommand(sheetSql1, conn))
                                {
                                    cmd.ExecuteNonQuery();
                                }
                                //}
                            }
                        }
                        // Write out row data
                        foreach (DataRow dr in dt.Rows)
                        {
                            string fields = "";
                            string values = "";
                            // now add records into the excel sheet just created
                            sheetSql1 = "INSERT INTO [" + data.DataSetName + "]";
                            for (int j = 0; j < maxcount; j++)
                            {
                                //fields += dt.Columns[j].ColumnName + ",";
                                //values += "@" + dt.Columns[j].ColumnName + ",";
                                fields += "F" + j.ToString() + ",";
                                values += "@F" + j.ToString() + ",";
                            }
                            fields = fields.Remove(fields.Length - 1);
                            values = values.Remove(values.Length - 1);
                            sheetSql1 += "(" + fields + ") VALUES(" + values + ")";
                            using (OleDbCommand cmd = new OleDbCommand(sheetSql1, conn))
                            {
                                for (int k = 0; k < maxcount; k++)
                                {

                                    string output;

                                    //Set appropriate styling of each cell based on datatype
                                    //This depends on how sql server ends up reporting the datatype.
                                    if (k < dt.Columns.Count)
                                    {
                                        switch (dt.Columns[k].DataType.ToString()) // FormatException murat data types
                                        {
                                            case "System.DateTime":
                                                try
                                                {

                                                    output = DateTime.Parse(dr[k].ToString()).ToString("dd/MM/yyyy");
                                                }
                                                catch (FormatException fe) //date is null or empty in dataset
                                                {
                                                    output = "";
                                                }

                                                break;
                                            case "System.Decimal":
                                            case "System.Double":
                                            case "System.Int16":
                                            case "System.Int32":
                                            case "System.Int64":
                                            case "System.Byte":

                                                output = dr[k].ToString().Trim();
                                                break;
                                            default:

                                                output = dr[k].ToString().Trim();
                                                break;
                                        }
                                    }
                                    else
                                        output = "0";
                                    //  cmd.Parameters.AddWithValue("@" + dt.Columns[k].ColumnName, dr[k]);

                                        cmd.Parameters.AddWithValue("@F" + k.ToString(), output);



                                }
                                cmd.ExecuteNonQuery();
                            }
                        }
                    }

                }

                conn.Close();
            }

i want to write sheets like this

0 个答案:

没有答案