Click here to Skip to main content
15,891,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good day all. I have 2 tables. One is filled by the other. In other words the e-mail addresses. And the other one has all the information including e-mail addresses. Now, i want to create multiple excel reports as per e-mail from the first table. So i need the program to loop through the first table and according to the value of the e-mail. Compare it to the second table and create a report according to all the information that is stored in the second table concerning the email address that the program is busy with. If you understand how i mean.

Could any one help me with this.

Thank you in advance.
:)
Posted
Comments
Tarun.K.S 7-Dec-10 5:16am    
Share your code. Where are you stuck?

What exactly is that you are stuck with?
Connecting to the db and retrieving data associated with an e-mail?
Or exporting the data to Excel?

There are plenty of articles for both needs.

Just search for "Export To Excel" inside CP, and I'm sure you'll
find what you need.

Or for "Database Access". :)
 
Share this answer
 
try
{

String MyConString = "Server=south-srv01;Database=jmds;Uid=root;Pwd=F1sh1ng;";

MySqlConnection sqlConn = new MySqlConnection(MyConString);

MySqlCommand cmd = sqlConn.CreateCommand();
MySqlCommand cmd1 = sqlConn.CreateCommand();

DataSet ds = new DataSet();
DataTable dt = new DataTable();

cmd.CommandText = "SELECT * FROM jmds.email";
sqlConn.Open();

MySqlDataAdapter adptr = new MySqlDataAdapter();
adptr.SelectCommand = cmd;
adptr.Fill(dt);



int rowCount = 1;

foreach (DataRow row in dt.Rows)
{
rowCount += 1;
// get the data
var1 = (string)row["DBEmail"];
//var1 = (string)row["DBEmail"];

Console.WriteLine(var1);

Excel.Application objApp;
Excel._Workbook objBook;

Excel.Workbooks objBooks;
Excel.Sheets objSheets;
Excel._Worksheet objSheet;
Excel.Range range;

objApp = new Excel.Application();
objBooks = objApp.Workbooks;
objBook = objBooks.Add(Missing.Value);
objSheets = objBook.Worksheets;
objSheet = (Excel._Worksheet)objSheets.get_Item(1);

range = objSheet.get_Range("A1", Missing.Value);

objApp.Cells.Replace("<br />", "", Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByRows, true, System.Type.Missing, false, false);
objApp.Cells.Replace("</a>", "", Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByRows, true, System.Type.Missing, false, false);
objApp.Cells.Replace("<a href=", "", Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByRows, true, System.Type.Missing, false, false);
objApp.Cells.Replace("target=", "", Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByRows, true, System.Type.Missing, false, false);
objApp.Cells.Replace("_blank", "", Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByRows, true, System.Type.Missing, false, false);
objApp.Cells.Replace("http://www.south-com.co.za", "", Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByRows, true, System.Type.Missing, false, false);
objApp.Cells.Replace(">&nbsp;", "", Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByRows, true, System.Type.Missing, false, false);
objApp.Visible = true;
cmd1.CommandText = "SELECT * FROM hesk_combined WHERE email = " + var1 + " ";
adptr.SelectCommand = cmd1;
adptr.Fill(dt);

for (int i = 1; i < dt.Columns.Count + 1; i++)
{

// Add the header the first time through
if (rowCount == 2)
{
objSheet.Cells[1, i] = dt.Columns[i - 1].ColumnName;

}
objSheet.Cells[rowCount, i] = var1[i - 1].ToString();

}
objBook.SaveCopyAs(@"C:\Mica_Report\LorenzoReport.xls");
}

sqlConn.Close();

}

catch (Exception e)
{
Console.WriteLine("Error " + e);
}


}
 
Share this answer
 
Well, i am stuck with reading the data according to the email.
My program makes the excel worksheet.
But there is no data in it.
I have posted the actual code i am stuck with.
If you want i can post the entire solution's code?

Thank you.
 
Share this answer
 
Did you check for the contents of the datatable? Are there any rows?
If not there might be a problem in your select statement.

Now a few hints/suggestions.
Don't use data adaptares, datasets...
Use a datareader. Also use parameters.
Ex. from a test project I'm currently working on:
C#
public static List<LinqClasses.Project> GetProjectsNonObs(bool obsolete)
       {
           //PROJECTS_SelectAllNonObs
           List<LinqClasses.Project> projects = null;
           LinqClasses.Project curr = null;

           SqlConnection con = new SqlConnection(Utils.Connections.GetReadConnection());
           SqlCommand cmd = new SqlCommand();
           cmd.Connection = con;
           cmd.CommandType = System.Data.CommandType.StoredProcedure;
           cmd.CommandText = "PROJECTS_SelectAllNonObs";
           cmd.Parameters.AddWithValue("@obs", obsolete);

           SqlDataReader dr = null;

           try
           {
               con.Open();
               dr = cmd.ExecuteReader();
               projects = new List<DataLayerLinq.LinqClasses.Project>();

               while (dr.Read())
               {
                   curr = new DataLayerLinq.LinqClasses.Project();
                   curr.ID = dr.GetInt32(0);
                   curr.Description = dr.GetString(1);
                   curr.PN = dr.GetString(2);
                   curr.AMG = dr.GetString(3);
                   curr.IsComponent = dr.GetBoolean(4);
                   curr.Obsolete = dr.GetBoolean(5);
                   curr.Price = dr.GetDouble(6);
                   projects.Add(curr);
               }
           }
           catch (Exception ex)
           {
               Utils.Logs.Write(ex, cmd);
               MessageBox.Show("Could not load all projects:\r\n" + ex.Message);
           }
           finally
           {
               if (con.State != System.Data.ConnectionState.Closed)
               {
                   con.Close();
               }
               con = null;
           }

           return projects;
       }


Where Project is a class that maps the sql table.
Doing things this way you have many advantages:
first you could debug in a more detailed way, you could check for each
project that is beeing read, a speedier version and finally more control to you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900