Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I want to dump data on excel sheet from data base,using dataset.
So, if i have multiple datatable or dataset in general. then how to seperate them. for eg: ABC is header Row and within ABC heading i have a various columns coming from dataset. and again i have a XYZ Title,with that also i have various columns coming from different dataset. So, How to separate ABC and XYZ programatically at same time while generating excel sheet.
Let me if can give some more clarification on the problem.
Thnx.
Posted

For generating the file automating Excel is probably the easiest option. A better option might be to simply write an office XML file.


But some clarification would be nice.
 
Share this answer
 
Ok,Let clarify my problem.
I want two or more range(combination of rows and columns) on the same sheet of the excel workbook. I want the two range to be filled by data coming from dataset
 
Share this answer
 
v2
Writing through XML is one of option available but my requirement is generate Excel in which data are coming from dataset. And i want to separate data by keeping them in differnt "range" of excel worksheet.
Let me know if some sample code can help us in better understanding of problem. Because i have developed a soltution which alredy working fine with one dataset and writing it on excel. but wat my requirement is data from two or more SPs should be dumbped on same excel sheet partioning with header and all. For that i simply want to know that how to iterate the excel cell or any better idea.
Reagrds.
 
Share this answer
 
Check out this article.
 
Share this answer
 
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp1_test";
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
cmd.ExecuteNonQuery();
con.Close();


*********************************************************
oXL = new Excel.Application();
oXL.Visible = true;
oXL.DisplayAlerts = false;

oWB = oXL.Workbooks.Add(Missing.Value);
Microsoft.Office.Interop.Excel.Style objStyle = oWB.Styles.Add("new", Missing.Value);
//Column names Color
objStyle.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DeepSkyBlue);

// Get the active sheet
oSheet = (Excel.Worksheet)oWB.ActiveSheet;
oSheet.Name = "Effort";
// Process the DataTable

System.Data.DataTable dt = ds.Tables[0];
int rowCount = 1;
foreach (DataRow dr in dt.Rows)
{
rowCount += 1;
for (int i = 1; i < dt.Columns.Count + 1; i++)
{
// Add the header the first time through
if (rowCount == 2)
{
oSheet.Cells[1, i] = dt.Columns[i - 1].ColumnName;
}
oSheet.Cells[rowCount, i] = dr[i - 1].ToString();
}
}
// Resize the columns
oRange = oSheet.get_Range(oSheet.Cells[1, 1],oSheet.Cells[rowCount, dt.Columns.Count]);
oRange.EntireColumn.AutoFit();
Range oRange2 = oSheet.get_Range(oSheet.Cells[1, 1], oSheet.Cells[1, dt.Columns.Count]);
oRange2.Style = objStyle;
oRange.Font.Bold = true;
//objRange.Value2 = Value1;
//oRange.Value2 = "35";
// Save the sheet and close
oSheet = null;
oRange = null;
oWB.SaveAs("testing1.xls", Excel.XlFileFormat.xlWorkbookNormal,Missing.Value, Missing.Value, Missing.Value, Missing.Value,Excel.XlSaveAsAccessMode.xlExclusive,Missing.Value, Missing.Value, Missing.Value,Missing.Value, Missing.Value);
//oWB.Close(Missing.Value, Missing.Value, Missing.Value);
//oWB = null;
//oXL.Quit();
// Clean up
// NOTE: When in release mode, this does the trick
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}


this my sample code. i hope,it will help us for better understanding of the problem.
 
Share this answer
 
"At the same time" would indicate doing this on multiple threads, but I suspect that that simply changing the 2 in the same <cod>for loop would suffice.

If you have the option to use the new office XML files (which would create an Office 2007 or better file), I would use that. That way it wouldn't require Excel to be deployed on the same machine, and you can use the Xml objects in System.Xml to create/edit the file.
 
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