944,005 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 1994
  • C# RSS
Sep 7th, 2009
0

Struggling on reading excel spreadsheet

Expand Post »
Hi Guys, i created an C# application, i need to read an excel spreadsheet called "Graduates.xlsx", i have to search for a specific row on a column called "ID number", once i get that column i must retrieve a record of that row on column "First name". Its been weeks trying this for some weeks.
Any help will be appreciated.
Similar Threads
Reputation Points: 7
Solved Threads: 1
Light Poster
Mongz is offline Offline
41 posts
since Apr 2009
Sep 7th, 2009
0

Re: Struggling on reading excel spreadsheet

why don't you get your complete excel sheet in a dataset and from there it should be easy...
i've done that...but I haven't tried it for ".xlsx", just for ".xls"
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zydaru is offline Offline
11 posts
since Aug 2009
Sep 7th, 2009
0

Re: Struggling on reading excel spreadsheet

Here is a class called "Excel Searcher":
C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Common;
  5. using System.IO;
  6.  
  7. namespace daniweb
  8. {
  9. public struct ExcelSearchResult
  10. {
  11. public int Row { get; set; }
  12. public int Col { get; set; }
  13. public string SheetName { get; set; }
  14. public ExcelSearchResult(int Row, int Col, string SheetName)
  15. : this()
  16. {
  17. this.Row = Row;
  18. this.Col = Col;
  19. this.SheetName = SheetName;
  20. }
  21. public override string ToString()
  22. {
  23. return string.Format("Sheet: '{0}', Row: {1:F0}, Column: {2:F0}",
  24. this.SheetName,
  25. this.Row,
  26. this.Col);
  27. }
  28. }
  29.  
  30. public class ExcelSeacher : IDisposable
  31. {
  32. private const string provider = @"System.Data.OleDb";
  33. private string _fileName;
  34. private string _connectionString;
  35. private bool _firstRowContainsColHeader;
  36. private DbProviderFactory factory;
  37. private DbConnection conn;
  38.  
  39. public string FileName
  40. {
  41. get { return _fileName; }
  42. }
  43. public string ConnectionString
  44. {
  45. get { return _connectionString; }
  46. }
  47. public bool FirstRowContainsColHeader
  48. {
  49. get { return _firstRowContainsColHeader; }
  50. }
  51. //
  52. #region ctors
  53. private ExcelSeacher()
  54. {
  55. factory = DbProviderFactories.GetFactory(provider);
  56. conn = factory.CreateConnection();
  57. }
  58. public ExcelSeacher(string FileName)
  59. : this(FileName, true)
  60. {
  61. }
  62. public ExcelSeacher(string FileName, bool FirstRowContainsColumnHeader)
  63. :this()
  64. {
  65. if (!File.Exists(FileName))
  66. throw new FileNotFoundException("File not found", FileName);
  67.  
  68. this._fileName = FileName;
  69. this._connectionString = BuildExcelConnectionString(FileName, FirstRowContainsColumnHeader);
  70. this._firstRowContainsColHeader = FirstRowContainsColumnHeader;
  71. conn.ConnectionString = this._connectionString;
  72. conn.Open();
  73. }
  74. #endregion
  75. private string[] GetTableList()
  76. {
  77. List<string> result = new List<string>();
  78. using (DataTable dtSchema = conn.GetSchema("Tables"))
  79. {
  80. for (int i1 = 0; i1 < dtSchema.Rows.Count; i1++)
  81. {
  82. string tblName = Convert.ToString(dtSchema.Rows[i1]["TABLE_NAME"]);
  83. string tblType = Convert.ToString(dtSchema.Rows[i1]["TABLE_TYPE"]);
  84. if ((string.Compare(tblType, "system", true) == 0) || (string.Compare(tblType, "access table", true) == 0))
  85. continue; //not a user table
  86. result.Add(tblName);
  87. }
  88. }
  89. return result.ToArray();
  90. }
  91.  
  92.  
  93. public ExcelSearchResult[] FindString(string TextToFind)
  94. {
  95. List<ExcelSearchResult> result = new List<ExcelSearchResult>();
  96.  
  97. string[] tableList = GetTableList();
  98. foreach (string tbl in tableList)
  99. {
  100. DataTable dt = GetTable(tbl);
  101. if (dt == null)
  102. continue;
  103. using (dt)
  104. {
  105. for (int iRow = 0; iRow < dt.Rows.Count; iRow++)
  106. {
  107. for (int iCol = 0; iCol < dt.Columns.Count; iCol++)
  108. {
  109. if (string.Compare(Convert.ToString(dt.Rows[iRow][iCol]), TextToFind, true) == 0)
  110. {
  111. /*
  112.   * Excel starts counting from 1, not zero. So we need to increate it by 1.
  113.   * Additionaly we need to increase it by another 1 if the first row contains
  114.   * column headers because in that case, the first row doesn't show up in
  115.   * dt.Rows
  116.   *
  117.   */
  118. if (this.FirstRowContainsColHeader)
  119. result.Add(new ExcelSearchResult(iRow+2, iCol, tbl));
  120. else
  121. result.Add(new ExcelSearchResult(iRow+1, iCol, tbl));
  122. }
  123. }
  124. }
  125. }
  126. }
  127.  
  128. return result.ToArray();
  129. }
  130.  
  131. private DbCommand GetCommand()
  132. {
  133. DbCommand cmd = factory.CreateCommand();
  134. cmd.Connection = conn;
  135. return cmd;
  136. }
  137.  
  138. private DataTable GetTable(string TableName)
  139. {
  140. try
  141. {
  142. string query = string.Format("Select * From [{0}]", TableName);
  143. using (DbCommand cmd = GetCommand())
  144. {
  145. cmd.CommandText = query;
  146. using (DbDataReader dr = cmd.ExecuteReader())
  147. {
  148. DataTable result = new DataTable();
  149. result.Load(dr);
  150. return result;
  151. }
  152. }
  153. }
  154. catch
  155. {
  156. return default(DataTable);
  157. }
  158. }
  159.  
  160. #region IDisposable Members
  161. public void Dispose()
  162. {
  163. if (conn != null)
  164. {
  165. conn.Dispose();
  166. conn = null;
  167. }
  168. }
  169. #endregion
  170.  
  171. private static string BuildExcelConnectionString(string Filename, bool FirstRowContainsHeaders)
  172. {
  173. return string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='{0}';Extended Properties=\"Excel 8.0;HDR={1};\"",
  174. Filename.Replace("'", "''"),
  175. FirstRowContainsHeaders ? "Yes" : "No");
  176. }
  177. }
  178. }

Calling it:
C# Syntax (Toggle Plain Text)
  1. private void button2_Click(object sender, EventArgs e)
  2. {
  3. ExcelSearchResult[] result;
  4. using (ExcelSeacher finder = new ExcelSeacher(@"C:\dotnetwin\xlApp\WindowsFormsApplication1\teknoser.xls", true))
  5. {
  6. result = finder.FindString("102500035");
  7. }
  8. foreach (ExcelSearchResult r in result)
  9. Console.WriteLine(r.ToString());
  10. System.Diagnostics.Debugger.Break();
  11. }
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 7th, 2009
0

Re: Struggling on reading excel spreadsheet

On this method i recieve an exception : "Invalid operation. The connection is closed."
#endregion
C# Syntax (Toggle Plain Text)
  1. private string[] GetTableList()
  2. {
  3. List<string> result = new List<string>();
  4. using (DataTable dtSchema = conn.GetSchema("Tables"))
  5. {
  6. for (int i1 = 0; i1 < dtSchema.Rows.Count; i1++)
  7. {
  8. string tblName = Convert.ToString(dtSchema.Rows[i1]["TABLE_NAME"]);
  9. string tblType = Convert.ToString(dtSchema.Rows[i1]["TABLE_TYPE"]);
  10. if ((string.Compare(tblType, "system", true) == 0) || (string.Compare(tblType, "access table", true) == 0))
  11. continue; //not a user table result.Add(tblName);
  12. }
  13. }
  14. return result.ToArray();
  15. }
  16.  
  17. And i changed path of the file to the following
  18.  
  19. protected void Button1_Click(object sender, EventArgs e)
  20. {
  21. Business.ExcelSearchResult[] result;
  22. using (Business.ExcelSeacher finder = new Business.ExcelSeacher(@"C:\Data\Graduates.xls", true))
  23. {
  24. result = finder.FindString("8009105987083");
  25. }
  26. foreach (Business.ExcelSearchResult r in result)
  27. MessageBox.Show(r.ToString());
  28. System.Diagnostics.Debugger.Break();
  29. }

please give some help, forgive me im still poor in programming
Last edited by John A; Sep 7th, 2009 at 1:29 pm. Reason: added code tags
Reputation Points: 7
Solved Threads: 1
Light Poster
Mongz is offline Offline
41 posts
since Apr 2009
Sep 7th, 2009
0

Re: Struggling on reading excel spreadsheet

Please use code tags when posting code on daniweb:

[code=csharp]
...code here....
[/code]


Next look at the commend I added in your code:
C# Syntax (Toggle Plain Text)
  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3. Business.ExcelSearchResult[] result;
  4. using (Business.ExcelSeacher finder = new Business.ExcelSeacher(@"C:\Data\Graduates.xls", true))
  5. {
  6. result = finder.FindString("8009105987083");
  7. } //The excel sheet is closed here!
  8. foreach (Business.ExcelSearchResult r in result)
  9. MessageBox.Show(r.ToString());
  10. System.Diagnostics.Debugger.Break();
  11. }

Finally, this code should work:
C# Syntax (Toggle Plain Text)
  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3. Business.ExcelSearchResult[] result;
  4. using (Business.ExcelSeacher finder = new Business.ExcelSeacher(@"C:\Data\Graduates.xls", true))
  5. {
  6. result = finder.FindString("8009105987083");
  7. foreach (Business.ExcelSearchResult r in result)
  8. {
  9. MessageBox.Show(r.ToString());
  10. System.Diagnostics.Debugger.Break();
  11. }
  12. }
  13. } //The excel sheet is closed here!

You need to access the excel sheets before you close the connection, which is done at the commented line because IDisposable.Dispose() is called on the ExcelSearcher
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 7th, 2009
0

Re: Struggling on reading excel spreadsheet

Can you please review the code becouse its not working for me.
[
Quote ...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using System.IO;
using System.Windows.Forms;

namespace Business
{
public struct ExcelSearchResult
{
public int Row { get; set; }
public int Col { get; set; }
public string SheetName { get; set; }
public ExcelSearchResult(int Row, int Col, string SheetName): this()
{
this.Row = Row;
this.Col = Col;
this.SheetName = SheetName;
}
public override string ToString()
{
return string.Format("Sheet: '{0}', Row: {1:F0}, Column: {2:F0}", this.SheetName, this.Row, this.Col);
}
}

public class ExcelSeacher : IDisposable
{
private const string provider = @"System.Data.OleDb";
private string _fileName;
private string _connectionString;
private bool _firstRowContainsColHeader;
private DbProviderFactory factory;
private DbConnection conn;

public string FileName
{
get { return _fileName; }
}

public string ConnectionString
{
get { return _connectionString; }
}

public bool FirstRowContainsColHeader
{
get { return _firstRowContainsColHeader; }
}

//
#region ctors

private ExcelSeacher()
{
factory = DbProviderFactories.GetFactory(provider);
conn = factory.CreateConnection();
}
public ExcelSeacher(string FileName): this(FileName, true)
{
}

public ExcelSeacher(string FileName, bool FirstRowContainsColumnHeader) :this()
{
try
{
if (!File.Exists(FileName))
throw new FileNotFoundException("File not found", FileName);
this._fileName = FileName;
this._connectionString = BuildExcelConnectionString(FileName, FirstRowContainsColumnHeader);
this._firstRowContainsColHeader = FirstRowContainsColumnHeader;
conn.ConnectionString = this._connectionString; conn.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

}

#endregion
private string[] GetTableList()
{
List<string> result = new List<string>();
try
{

using (DataTable dtSchema = conn.GetSchema("Tables"))
{
for (int i1 = 0; i1 < dtSchema.Rows.Count; i1++)
{
string tblName = Convert.ToString(dtSchema.Rows[i1]["TABLE_NAME"]);
string tblType = Convert.ToString(dtSchema.Rows[i1]["TABLE_TYPE"]);
if ((string.Compare(tblType, "system", true) == 0) || (string.Compare(tblType, "access table", true) == 0))
continue; //not a user table result.Add(tblName);
}
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return result.ToArray();
}

public ExcelSearchResult[] FindString(string TextToFind)
{
List<ExcelSearchResult> result = new List<ExcelSearchResult>();
string[] tableList = GetTableList();
foreach (string tbl in tableList)
{
DataTable dt = GetTable(tbl);
if (dt == null)
continue;
using (dt)
{
for (int iRow = 0; iRow < dt.Rows.Count; iRow++)
{
for (int iCol = 0; iCol < dt.Columns.Count; iCol++)
{
if (string.Compare(Convert.ToString(dt.Rows[iRow][iCol]), TextToFind, true) == 0)
{
/*
* * Excel starts counting from 1, not zero. So we need to increate it by 1.
* * Additionaly we need to increase it by another 1 if the first row contains
* * column headers because in that case, the first row doesn't show up in
* * dt.Rows
* *
* */
if (this.FirstRowContainsColHeader)
result.Add(new ExcelSearchResult(iRow+2, iCol, tbl));
else
result.Add(new ExcelSearchResult(iRow+1, iCol, tbl));
}
}
}
}
}
return result.ToArray();
}



private DbCommand GetCommand()
{
DbCommand cmd = factory.CreateCommand();
cmd.Connection = conn;
return cmd;
}

private DataTable GetTable(string TableName)
{
try
{
string query = string.Format("Select * From [{0}]", TableName);
using (DbCommand cmd = GetCommand())
{
cmd.CommandText = query;
using (DbDataReader dr = cmd.ExecuteReader())
{
DataTable result = new DataTable();
result.Load(dr);
return result;
}
}
}
catch
{
return default(DataTable);
}
}

#region IDisposable Members
public void Dispose()
{
if (conn != null)
{
conn.Dispose();
conn = null;
}
}

#endregion
private static string BuildExcelConnectionString(string Filename, bool FirstRowContainsHeaders)
{
return string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='{0}';Extended Properties=\"Excel 8.0;HDR={1};\"",Filename.Replace("'", "''"),FirstRowContainsHeaders ? "Yes" : "No");
}

}
}
Then this is behind the button. The file is on c drive inside folder called Data. I recieve an exception " File not found" and "connectionstring not open.
Quote ...
protected void Button1_Click(object sender, EventArgs e)
{
Business.ExcelSearchResult[] result;
using (Business.ExcelSeacher finder = new Business.ExcelSeacher(@"C:\Data\Graduates.xls", true))
{
result = finder.FindString("8009105987083");
foreach (Business.ExcelSearchResult r in result)
{
MessageBox.Show(r.ToString());
System.Diagnostics.Debugger.Break();
}
}
}
Your kindness is really appreciated
Reputation Points: 7
Solved Threads: 1
Light Poster
Mongz is offline Offline
41 posts
since Apr 2009
Sep 7th, 2009
0

Re: Struggling on reading excel spreadsheet

[CODE=csharp]private DataTable getDataFromXLS(string strFilePath)
{
string strConnectionString = "";
strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + strFilePath + "; Jet OLEDB:Engine Type=5;" +
"Extended Properties=Excel 8.0;";
OleDbConnection cnCSV = new OleDbConnection(strConnectionString);
cnCSV.Open();
OleDbCommand cmdSelect = new OleDbCommand(@"SELECT * FROM [Sheet1$]", cnCSV);
OleDbDataAdapter daCSV = new OleDbDataAdapter(); daCSV.SelectCommand = cmdSelect;
DataTable dtCSV = new DataTable();
daCSV.Fill(dtCSV);
cnCSV.Close();
daCSV = null;
return dtCSV;
}[/CODE=csharp]

and from a DataTable you can do whatever you want ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zydaru is offline Offline
11 posts
since Aug 2009
Sep 7th, 2009
0

Re: Struggling on reading excel spreadsheet

Ah I lied -- and gave you the incorrect call. The problem was that I gave you a connection string for ".xls" -- and not ".xlsx". Also the code I gave you only did _exact_ matches. I have changed it to work with .xls and .xlsx, as well as partial matches. Please see this code:

Excel search:
c# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Common;
  5. using System.IO;
  6.  
  7. namespace daniweb
  8. {
  9. public struct ExcelSearchResult
  10. {
  11. public int Row { get; set; }
  12. public int Col { get; set; }
  13. public string SheetName { get; set; }
  14. public ExcelSearchResult(int Row, int Col, string SheetName)
  15. : this()
  16. {
  17. this.Row = Row;
  18. this.Col = Col;
  19. this.SheetName = SheetName;
  20. }
  21. public override string ToString()
  22. {
  23. return string.Format("Sheet: '{0}', Row: {1:F0}, Column: {2:F0}",
  24. this.SheetName,
  25. this.Row,
  26. this.Col);
  27. }
  28. }
  29.  
  30. public class ExcelSeacher : IDisposable
  31. {
  32. private const string provider = @"System.Data.OleDb";
  33. private string _fileName;
  34. private string _connectionString;
  35. private bool _firstRowContainsColHeader;
  36. private DbProviderFactory factory;
  37. private DbConnection conn;
  38.  
  39. public string FileName
  40. {
  41. get { return _fileName; }
  42. }
  43. public string ConnectionString
  44. {
  45. get { return _connectionString; }
  46. }
  47. public bool FirstRowContainsColHeader
  48. {
  49. get { return _firstRowContainsColHeader; }
  50. }
  51. //
  52. #region ctors
  53. private ExcelSeacher()
  54. {
  55. factory = DbProviderFactories.GetFactory(provider);
  56. conn = factory.CreateConnection();
  57. }
  58. public ExcelSeacher(string FileName)
  59. : this(FileName, true)
  60. {
  61. }
  62. public ExcelSeacher(string FileName, bool FirstRowContainsColumnHeader)
  63. :this()
  64. {
  65. if (!File.Exists(FileName))
  66. throw new FileNotFoundException("File not found", FileName);
  67.  
  68. string fileExt = Path.GetExtension(FileName);
  69.  
  70.  
  71. this._fileName = FileName;
  72.  
  73. if (string.Compare(fileExt, ".xls", true) == 0)
  74. this._connectionString = BuildExcelConnectionString(FileName, FirstRowContainsColumnHeader);
  75. else if (string.Compare(fileExt, ".xlsx", true) == 0)
  76. this._connectionString = BuildExcel2007ConnectionString(FileName, FirstRowContainsColumnHeader);
  77. else
  78. throw new InvalidOperationException("Unknown file extension");
  79.  
  80. this._firstRowContainsColHeader = FirstRowContainsColumnHeader;
  81. conn.ConnectionString = this._connectionString;
  82. conn.Open();
  83. }
  84. #endregion
  85. private string[] GetTableList()
  86. {
  87. List<string> result = new List<string>();
  88. using (DataTable dtSchema = conn.GetSchema("Tables"))
  89. {
  90. for (int i1 = 0; i1 < dtSchema.Rows.Count; i1++)
  91. {
  92. string tblName = Convert.ToString(dtSchema.Rows[i1]["TABLE_NAME"]);
  93. string tblType = Convert.ToString(dtSchema.Rows[i1]["TABLE_TYPE"]);
  94. if ((string.Compare(tblType, "system", true) == 0) || (string.Compare(tblType, "access table", true) == 0))
  95. continue; //not a user table
  96. result.Add(tblName);
  97. }
  98. }
  99. return result.ToArray();
  100. }
  101.  
  102. /// <summary>
  103. ///
  104. /// </summary>
  105. /// <param name="TextToFind">Search term</param>
  106. /// <param name="ExactMatch">Match all of the cell contents</param>
  107. /// <param name="IgnoreCase">Whether or not the case should be ignored</param>
  108. /// <returns></returns>
  109. public ExcelSearchResult[] FindString(string TextToFind, bool ExactMatch, bool IgnoreCase)
  110. {
  111. List<ExcelSearchResult> result = new List<ExcelSearchResult>();
  112.  
  113. string[] tableList = GetTableList();
  114. foreach (string tbl in tableList)
  115. {
  116. DataTable dt = GetTable(tbl);
  117. if (dt == null)
  118. continue;
  119. using (dt)
  120. {
  121. for (int iRow = 0; iRow < dt.Rows.Count; iRow++)
  122. {
  123. for (int iCol = 0; iCol < dt.Columns.Count; iCol++)
  124. {
  125. string cellData = Convert.ToString(dt.Rows[iRow][iCol]);
  126. if (cellData == null)
  127. cellData = string.Empty;
  128.  
  129. bool matched;
  130.  
  131. if (ExactMatch)
  132. matched = (string.Compare(cellData, TextToFind, IgnoreCase) == 0);
  133. else
  134. matched = (cellData.IndexOf(TextToFind, (IgnoreCase ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture)) >= 0);
  135.  
  136. if (matched)
  137. {
  138. /*
  139.   * Excel starts counting from 1, not zero. So we need to increate it by 1.
  140.   * Additionaly we need to increase it by another 1 if the first row contains
  141.   * column headers because in that case, the first row doesn't show up in
  142.   * dt.Rows
  143.   *
  144.   */
  145. if (this.FirstRowContainsColHeader)
  146. result.Add(new ExcelSearchResult(iRow+2, iCol, tbl));
  147. else
  148. result.Add(new ExcelSearchResult(iRow+1, iCol, tbl));
  149. }
  150. }
  151. }
  152. }
  153. }
  154.  
  155. return result.ToArray();
  156. }
  157.  
  158. private DbCommand GetCommand()
  159. {
  160. DbCommand cmd = factory.CreateCommand();
  161. cmd.Connection = conn;
  162. return cmd;
  163. }
  164.  
  165. private DataTable GetTable(string TableName)
  166. {
  167. try
  168. {
  169. string query = string.Format("Select * From [{0}]", TableName);
  170. using (DbCommand cmd = GetCommand())
  171. {
  172. cmd.CommandText = query;
  173. using (DbDataReader dr = cmd.ExecuteReader())
  174. {
  175. DataTable result = new DataTable();
  176. result.Load(dr);
  177. return result;
  178. }
  179. }
  180. }
  181. catch
  182. {
  183. return default(DataTable);
  184. }
  185. }
  186.  
  187. #region IDisposable Members
  188. public void Dispose()
  189. {
  190. if (conn != null)
  191. {
  192. conn.Dispose();
  193. conn = null;
  194. }
  195. }
  196. #endregion
  197.  
  198. private static string BuildExcelConnectionString(string Filename, bool FirstRowContainsHeaders)
  199. {
  200. return string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='{0}';Extended Properties=\"Excel 8.0;HDR={1};\"",
  201. Filename.Replace("'", "''"),
  202. FirstRowContainsHeaders ? "Yes" : "No");
  203. }
  204. public static string BuildExcel2007ConnectionString(string Filename, bool FirstRowContainsHeaders)
  205. {
  206. return string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0;HDR={1}\";",
  207. Filename.Replace("'", "''"),
  208. FirstRowContainsHeaders ? "Yes" : "No");
  209. }
  210. }
  211. }

Calling it:
c# Syntax (Toggle Plain Text)
  1. private void button4_Click(object sender, EventArgs e)
  2. {
  3. ExcelSearchResult[] result;
  4. using (ExcelSeacher finder = new ExcelSeacher(@"C:\data\Spreadsheet.xls", true))
  5. {
  6. result = finder.FindString("o", false, true);
  7. }
  8. foreach (ExcelSearchResult r in result)
  9. {
  10. Console.WriteLine(r.ToString());
  11. }
  12. }
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 7th, 2009
0

Re: Struggling on reading excel spreadsheet

Now the code can read but bit complicated to make sum changes becouse it returns : "Sheet: 'Sheet1$',Row:3, Column:0", "Sheet: 'Sheet1$',Row:3, Column:0" and "Sheet: 'Sheet1$',Row:3, Column:0" then it terminates. Cant read the cell i want
Reputation Points: 7
Solved Threads: 1
Light Poster
Mongz is offline Offline
41 posts
since Apr 2009
Sep 7th, 2009
0

Re: Struggling on reading excel spreadsheet

Yes that code shows you how to search every cell.. change the excel searcher to grab the other column and stick the information you want in the result[] array it returns.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Create a password protected .mdb file
Next Thread in C# Forum Timeline: System Running time





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC