Microsoft.ACE.OLEDB.12.0(64位):在2000行之后插入EXCEL .xlsm返回空白表 [英] Microsoft.ACE.OLEDB.12.0 ( 64-bit ) : Insert into EXCEL .xlsm return blank sheet after 2000 rows

查看:41
本文介绍了Microsoft.ACE.OLEDB.12.0(64位):在2000行之后插入EXCEL .xlsm返回空白表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在使用Microsoft.ACE.OLEDB.12.0(64位)时遇到以下问题,请建议:





Excel文件有很多页和大量的列,但没有。这些工作表上的行数小于2000,因此除了没有的工作表之外,所有工作表都很好。行数超过2000.在本地环境中所有工作正常,但在IIS(也在LIVE上)托管后它失败了。

我已经在LIVE服务器上尝试过调试模式(使用visual studio)并且它可以正常工作。但是通过IIS的相同站点文件失败。



环境:

- AccessDatabaseEngine 2010(x64)+ SP1

- Windows Server 2008 R2企业版(64位)

- Microsoft SQL Server 2008 R2企业版(64位)

- IIS 7

- 简单的Excel_Template.xlsm,列布局A-AM



脚本(使用ASP.NET - C#)从MSSQL检索选定的数据并插入到Excel模板中文件。



如果数据集/行数小于2000,则一切正常。

200X以后脚本开始运行不正常:

- 调试时没有异常

- OleDbCommand.ExecuteNonQuery成功(在整个循环中返回指标1)

- 如果在2000行休息一下count然后excel文件.xlsm生成成功

- 如果在2010行数中断,则生成excel文件.xlsm但输出文件为空(完全是s在插入任何行/数据之前作为模板文件)作为模板文件

- 所有进程,循环和连接都正常工作。

- 没有数据或格式问题,因为剩余的行有类似的长度和模式



脚本类似于以下内容:



Hi All,

I encountered the following issue with Microsoft.ACE.OLEDB.12.0 ( 64-bit ), kindly advise :


Excel file has many sheets and huge number of columns but no. of rows on these worksheets are less than 2000, hence all worksheet are filling well except one having no. of rows are more then 2000. On local environment all working fine, but after hosting on IIS (on LIVE also) it's failing.
I have tried on LIVE server on debug mode (using visual studio) and it worked their. But the same site files via IIS failing.

Environment :
- AccessDatabaseEngine 2010 ( x64 ) + SP1
- Windows Server 2008 R2 Enterprise ( 64-bit )
- Microsoft SQL Server 2008 R2 Enterprise Edition (64-bit)
- IIS 7
- Simple Excel_Template.xlsm with columns layout A-AM

Scripting ( with ASP.NET - C# ) to retrieve selected data from MSSQL and insert into the excel template file.

Everything working FINE if the dataset / number of rows less than 2000.
The scripts started not behave correctly after 200X onwards :
- No exception when debugging
- OleDbCommand.ExecuteNonQuery successful ( return indicator 1 throughout entire loops )
- If put a break at 2000 row count then excel file .xlsm was generated successfully
- If put a break at 2010 row count then excel file .xlsm was generated but output file is blank ( exactly same as template file before insert any row/data )
- All processes, loops and connections are working fine.
- No data or formatting issue because the remaining rows having similar length and pattern

The scripts are similar to the below :

public static bool fillExcelFile(string filePath, DataSet dataSet, string sheetName, string tempSheetName)
    {


        DataTable oledbdatatbl = null;
        using (OleDbDataAdapter oledbadap = new OleDbDataAdapter(string.Format("SELECT * FROM [{0}$]", sheetName),
                  string.Format(@"Data Source={0};Provider=Microsoft.ACE.OLEDB.12.0; Extended Properties=Excel 12.0;", filePath)))
        {
            try
            {
                oledbdatatbl = new System.Data.DataTable("ReportTable");
                oledbadap.Fill(oledbdatatbl);
            }
            catch (Exception ex)
            {
                Error_SendMail(ex);
                return false;
            }
        }






        System.Text.StringBuilder sbColumns = new System.Text.StringBuilder();
        int coloumsCount = 0;


        for (int icol = 0; icol < (oledbdatatbl.Columns.Count); icol++)
        {
            coloumsCount++;
            sbColumns.Append(string.Format(",[{0}]", oledbdatatbl.Columns[icol].ColumnName));

        }

        if (sbColumns.Length > 0)
            sbColumns.Remove(0, 1);




        using (OleDbConnection oledbConn = new OleDbConnection(string.Format(@"Data Source={0};Provider=Microsoft.ACE.OLEDB.12.0; Extended Properties=Excel 12.0;", filePath)))
        {
            using (OleDbCommand oledbCmd = new OleDbCommand())
            {
                oledbConn.Open();

                System.Text.StringBuilder sbValues = new System.Text.StringBuilder();
                int valuesCount = 0;

                for (int irow = 0; irow < dataSet.Tables[0].Rows.Count; irow++)
                {
                    if (sbValues.Length > 0)
                        sbValues.Remove(0, sbValues.Length);

                    try
                    {
                        valuesCount = 0;
                        for (int idatacol = 0; idatacol < dataSet.Tables[0].Columns.Count; idatacol++)
                        {
                            valuesCount++;

                            sbValues.Append(string.Format(",'{0}'", Convert.ToString(dataSet.Tables[0].Rows[irow][idatacol]).Contains("'") ?
                                Convert.ToString(dataSet.Tables[0].Rows[irow][idatacol]).Replace("'", "''") :
                                Convert.ToString(dataSet.Tables[0].Rows[irow][idatacol])));
                        }

                        if (sbValues.Length > 0)
                            sbValues.Remove(0, 1);
                    }
                    catch (Exception ex)
                    {
                        Error_SendMail(ex);
                        continue;
                    }


                    if (coloumsCount == valuesCount)
                    {
                        try
                        {
                            oledbCmd.CommandText = string.Format("insert into [{0}$]({1})values({2})", tempSheetName, sbColumns.ToString(), sbValues.ToString());
                            oledbCmd.CommandType = System.Data.CommandType.Text;

                            if (oledbConn.State == System.Data.ConnectionState.Closed)
                                oledbConn.Open();

                            oledbCmd.Connection = oledbConn;
                            oledbCmd.ExecuteNonQuery();
                        }
                        catch (Exception ex)
                        {

                            if (oledbConn.State == System.Data.ConnectionState.Open)
                                oledbConn.Close();
                            Error_SendMail(ex);
                        }
                    }
                }

                if (oledbConn.State == System.Data.ConnectionState.Open)
                    oledbConn.Close();
            }
        }
        return true;
    }

推荐答案

,sheetName),
string .Format( @ 数据源= {0};提供者= Microsoft.ACE.OLEDB .12.0;扩展属性= Excel 12.0;,filePath)))
{
尝试
{
oledbdatatbl = new System.Data.DataTable( ReportTable);
oledbadap.Fill(oledbdatatbl);
}
catch (Exception ex)
{
Error_SendMail(ex);
return false ;
}
}






System.Text.String Builder sbColumns = new System.Text.StringBuilder();
int coloumsCount = 0 ;


for int icol = 0 ; icol < (oledbdatatbl.Columns.Count); icol ++)
{
coloumsCount ++;
sbColumns.Append( string .Format( ,[{0}],oledbdatatbl.Columns [icol] .ColumnName));

}

if (sbColumns.Length > 0
sbColumns.Remove( 0 1 );




使用(OleDbConnection oledbConn = new OleDbConnection( string .Format( @ 数据源= {0};提供者= Microsoft.ACE.OLEDB.12.0;扩展属性= Excel 12.0;,filePath)))
{
使用(OleDbCommand oledbCmd = new OleDbCommand())
{
oledbConn.Open();

System.Text.StringBuilder sbValues = new System.Text.StringBuilder();
int valuesCount = 0 ;

for int irow = 0 ; irow < dataSet.Tables [ 0 ]。Rows.Count; irow ++ )
{
if (sbValues.Length > 0
sbValues.Remove( 0 ,sbValues.Length);

尝试
{
valuesCount = 0 ;
for int idatacol = 0 ; idatacol < dataSet.Tables [ 0 ]。Columns.Count; idatacol ++)
{
valuesCount ++;

sbValues.Append( string .Format( ,'{0}',Convert.ToString(dataSet.Tables [ 0 ]。行[irow] [idatacol]) .Contains( ')?
Convert.ToString(dataSet.Tables [ 0 ]。行[irow] [idatacol])。替换( ' ''):
Convert.ToString( dataSet.Tables [ 0 ]。行[irow] [idatacol])));
}

如果(sbValues.Length > 0
sbValues.Remove( 0 1 < /跨度>);
}
catch (例外情况)
{
Error_SendMail(ex);
继续;
}


if (coloumsCount == valuesCount)
{
尝试
{
oledbCmd.CommandText = string .Format( 插入[{0}
", sheetName), string.Format(@"Data Source={0};Provider=Microsoft.ACE.OLEDB.12.0; Extended Properties=Excel 12.0;", filePath))) { try { oledbdatatbl = new System.Data.DataTable("ReportTable"); oledbadap.Fill(oledbdatatbl); } catch (Exception ex) { Error_SendMail(ex); return false; } } System.Text.StringBuilder sbColumns = new System.Text.StringBuilder(); int coloumsCount = 0; for (int icol = 0; icol < (oledbdatatbl.Columns.Count); icol++) { coloumsCount++; sbColumns.Append(string.Format(",[{0}]", oledbdatatbl.Columns[icol].ColumnName)); } if (sbColumns.Length > 0) sbColumns.Remove(0, 1); using (OleDbConnection oledbConn = new OleDbConnection(string.Format(@"Data Source={0};Provider=Microsoft.ACE.OLEDB.12.0; Extended Properties=Excel 12.0;", filePath))) { using (OleDbCommand oledbCmd = new OleDbCommand()) { oledbConn.Open(); System.Text.StringBuilder sbValues = new System.Text.StringBuilder(); int valuesCount = 0; for (int irow = 0; irow < dataSet.Tables[0].Rows.Count; irow++) { if (sbValues.Length > 0) sbValues.Remove(0, sbValues.Length); try { valuesCount = 0; for (int idatacol = 0; idatacol < dataSet.Tables[0].Columns.Count; idatacol++) { valuesCount++; sbValues.Append(string.Format(",'{0}'", Convert.ToString(dataSet.Tables[0].Rows[irow][idatacol]).Contains("'") ? Convert.ToString(dataSet.Tables[0].Rows[irow][idatacol]).Replace("'", "''") : Convert.ToString(dataSet.Tables[0].Rows[irow][idatacol]))); } if (sbValues.Length > 0) sbValues.Remove(0, 1); } catch (Exception ex) { Error_SendMail(ex); continue; } if (coloumsCount == valuesCount) { try { oledbCmd.CommandText = string.Format("insert into [{0}


({1})值({2}),tempSheetName,sbColumns.ToString(),sbValues.ToString());
oledbCmd.CommandType = System.Data.CommandType.Text;

if (oledbConn.State == System.Data.ConnectionState.Closed)
oledbConn.Open();

oledbCmd.Connection = oledbConn;
oledbCmd.ExecuteNonQuery();
}
catch (例外情况)
{

if (oledbConn.State == System.Data.ConnectionState.Open)
oledbConn.Close();
Error_SendMail(ex);
}
}
}

if (oledbConn.State == System.Data.ConnectionState。打开)
oledbConn.Close();
}
}
返回 true ;
}
({1})values({2})", tempSheetName, sbColumns.ToString(), sbValues.ToString()); oledbCmd.CommandType = System.Data.CommandType.Text; if (oledbConn.State == System.Data.ConnectionState.Closed) oledbConn.Open(); oledbCmd.Connection = oledbConn; oledbCmd.ExecuteNonQuery(); } catch (Exception ex) { if (oledbConn.State == System.Data.ConnectionState.Open) oledbConn.Close(); Error_SendMail(ex); } } } if (oledbConn.State == System.Data.ConnectionState.Open) oledbConn.Close(); } } return true; }


这篇关于Microsoft.ACE.OLEDB.12.0(64位):在2000行之后插入EXCEL .xlsm返回空白表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆