Ssystem内存不足异常 [英] Ssystem out of memory exception
本文介绍了Ssystem内存不足异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经编写了我的代码,将Excel转换为其他格式的分隔符,但它抛出System.outof内存异常
我的代码如下所示
请帮我这个
我尝试过:
I have write my code to convert Excel to other format with delimiters but its throwing System.outof memory Exception
My Code is Like below
Please Help me on this
What I have tried:
<pre>string excelConnStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + fileName + ";Extended Properties=Excel 12.0;";
OleDbConnection excelConn = new OleDbConnection(excelConnStr);
excelConn.Open();
DataTable dbSchema = excelConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string strsheetName = "Sheet1$";
if (dbSchema != null && dbSchema.Rows.Count > 0)
{
strsheetName = Convert.ToString(dbSchema.Rows[0]["TABLE_NAME"]);
}
System.Data.DataTable dtPatterns = new System.Data.DataTable();
excelCommand = new OleDbCommand("SELECT * FROM [" + strsheetName + "]", excelConn);
excelDataAdapter.SelectCommand = excelCommand;
excelDataAdapter.Fill(dtPatterns);
List<string> xcelData = new List<string>();
List<string> ColumnNames = new List<string>();
ds.Tables.Add(dtPatterns);
totalRowsCount = dtPatterns.Rows.Count;
totalColumnsCount = dtPatterns.Columns.Count;
for (int c = 0; c < dtPatterns.Columns.Count; c++)
{
if (c == dtPatterns.Columns.Count - 1)
{
SB1.Append(cValue.ToString() + dtPatterns.Columns[c].ToString() + cValue.ToString());
}
else
{
SB1.Append(cValue.ToString() + dtPatterns.Columns[c].ToString() + cValue.ToString() + sValue.ToString());
}
}
SB1.Append("\r\n");
for (int d = 0; d < dtPatterns.Rows.Count; d++)
{
this.toolStripStatusLabel1.Text = string.Format("Processing Please Wait ...{0} of {1}", (count + 1), totalRowsCount);
for (int c = 0; c < dtPatterns.Columns.Count; c++)
{
int g = dtPatterns.Columns.Count - 1;
if (c == g)
{
SB1.Append(cValue.ToString() + dtPatterns.Rows[d][c].ToString() + cValue.ToString());
}
else
{
SB1.Append(cValue.ToString() + dtPatterns.Rows[d][c].ToString() + cValue.ToString() + sValue.ToString());
}
}
SB1.Append("\r\n");
count++;
}
推荐答案
;
if(dbSchema!= null&& dbSchema.Rows.Count > 0)
{
strsheetName = Convert.ToString(dbSchema.Rows [0] [TABLE_NAME]);
}
System.Data.DataTable dtPatterns = new System.Data.DataTable();
excelCommand = new OleDbCommand(SELECT * FROM [+ strsheetName +],excelConn);
excelDataAdapter.SelectCommand = excelCommand;
excelDataAdapter.Fill(dtPatterns);
List< string> xcelData = new List< string>();
List< string> ColumnNames = new List< string>();
ds.Tables.Add(dtPatterns);
totalRowsCount = dt Patterns.Rows.Count;
totalColumnsCount = dtPatterns.Columns.Count;
for(int c = 0; c< dtPatterns.Columns.Count; c ++)
{
if(c == dtPatterns.Columns.Count - 1)
{
SB1.Append(cValue.ToString()+ dtPatterns.Columns [c] .ToString()+ cValue.ToString());
}
其他
{
SB1.Append(cValue.ToString()+ dtPatterns.Columns [c] .ToString()+ cValue.ToString()+ sValue.ToString( ));
}
}
SB1.Append(\\\\ n);
for(int d = 0; d< dtPatterns.Rows.Count; d ++)
{
this.toolStripStatusLabel1.Text = string.Format(Processing Please Wait ... {0 } {1},(count + 1),totalRowsCount);
for(int c = 0; c< dtPatterns.Columns.Count; c ++)
{
int g = dtPatterns.Columns.Count - 1;
if(c == g)
{
SB1.Append(cValue.ToString()+ dtPatterns.Rows [d] [c] .ToString()+ cValue.ToString()) ;
}
其他
{
SB1.Append(cValue.ToString()+ dtPatterns.Rows [d] [c] .ToString()+ cValue.ToString()+ sValue的ToString());
}
}
SB1.Append(\\\\ n);
count ++;
}
"; if (dbSchema != null && dbSchema.Rows.Count > 0) { strsheetName = Convert.ToString(dbSchema.Rows[0]["TABLE_NAME"]); } System.Data.DataTable dtPatterns = new System.Data.DataTable(); excelCommand = new OleDbCommand("SELECT * FROM [" + strsheetName + "]", excelConn); excelDataAdapter.SelectCommand = excelCommand; excelDataAdapter.Fill(dtPatterns); List<string> xcelData = new List<string>(); List<string> ColumnNames = new List<string>(); ds.Tables.Add(dtPatterns); totalRowsCount = dtPatterns.Rows.Count; totalColumnsCount = dtPatterns.Columns.Count; for (int c = 0; c < dtPatterns.Columns.Count; c++) { if (c == dtPatterns.Columns.Count - 1) { SB1.Append(cValue.ToString() + dtPatterns.Columns[c].ToString() + cValue.ToString()); } else { SB1.Append(cValue.ToString() + dtPatterns.Columns[c].ToString() + cValue.ToString() + sValue.ToString()); } } SB1.Append("\r\n"); for (int d = 0; d < dtPatterns.Rows.Count; d++) { this.toolStripStatusLabel1.Text = string.Format("Processing Please Wait ...{0} of {1}", (count + 1), totalRowsCount); for (int c = 0; c < dtPatterns.Columns.Count; c++) { int g = dtPatterns.Columns.Count - 1; if (c == g) { SB1.Append(cValue.ToString() + dtPatterns.Rows[d][c].ToString() + cValue.ToString()); } else { SB1.Append(cValue.ToString() + dtPatterns.Rows[d][c].ToString() + cValue.ToString() + sValue.ToString()); } } SB1.Append("\r\n"); count++; }
使用StringBuilder保存整个电子表格的另一种方法是直接写入文本文件 - 参见如何:写入文本文件(C#编程指南)| Microsoft Docs [ ^ ]。在将每行写入文件之前,可以使用StringBuilder(这将有助于加快速度)。你应该为每个循环使用
An alternative to using a StringBuilder to hold the entire spreadsheet is to write straight out to a textfile - see How to: Write to a Text File (C# Programming Guide) | Microsoft Docs[^]. You can use a StringBuilder for each row before you write it to the file (which will help to speed it up). You should use
sb.Clear();
。
如果你不想制作你的现有代码发生了很大的变化(我们无法运行 - 有太多的声明等缺失),然后用一个只处理1000行的循环来围绕dataread ...
for each loop.
If you don't want to make that much of a change to your existing code (which we can't run - there are too many declarations etc missing), then surround the dataread with a loop that only handles, say, 1000 rows...
StringBuilder sb = new StringBuilder();
for (int i = 0; i <= rowcount; i += 1000)
{
// in here handle only 1000 rows from the spreadsheet
//After handling the 1000 rows ...
// Write the contents of sb to the file
// then do this sb.Clear(); or if you don't have that version of .NET...
sb = new StringBuilder();
}
这篇关于Ssystem内存不足异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文