Click here to Skip to main content
15,914,416 members
Home / Discussions / C#
   

C#

 
AnswerRe: Time Difference calculator Pin
musefan20-May-09 22:10
musefan20-May-09 22:10 
AnswerRe: Time Difference calculator Pin
Simon P Stevens20-May-09 22:46
Simon P Stevens20-May-09 22:46 
QuestionHow to change the body tag attributes in c# using ? Pin
svt gdwl20-May-09 21:09
svt gdwl20-May-09 21:09 
AnswerRe: How to change the body tag attributes in c# using ? Pin
Mycroft Holmes20-May-09 21:15
professionalMycroft Holmes20-May-09 21:15 
QuestionMicrosoft.Office.Interop.Excel.Application issue [modified] Pin
AndieDu20-May-09 20:56
AndieDu20-May-09 20:56 
AnswerRe: Microsoft.Office.Interop.Excel.Application issue Pin
MumbleB20-May-09 21:40
MumbleB20-May-09 21:40 
GeneralRe: Microsoft.Office.Interop.Excel.Application issue Pin
AndieDu21-May-09 0:09
AndieDu21-May-09 0:09 
AnswerRe: Microsoft.Office.Interop.Excel.Application issue Pin
AmmySA20-May-09 22:38
AmmySA20-May-09 22:38 
Hi AndieDu

I did not read ur requirement fully.
But i designed the similar code and I got the data from the Database and stored the values
in the excel tablewise.

I think It may help you.



using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient ;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using Excel_12 = Microsoft.Office.Interop.Excel;

namespace WindowsExcelSheet
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}



private void button1_Click(object sender, EventArgs e)
{
Excel_12.Application oXL;
Excel_12._Workbook oWB;
Excel_12._Worksheet oSheet;

try
{
// Start Excel And get Application Object.
oXL = new Excel_12.Application();
oXL.Visible = true;
// Get a new Workbook
oWB = (Excel_12._Workbook)(oXL.Workbooks.Add(Missing.Value));
oSheet = (Excel_12._Worksheet)oWB.ActiveSheet;
// Add Table Headers going Cell by Cell
//oSheet.Cells[1, 1] = "SaleID";
//oSheet.Cells[1, 2] = "Product";
//oSheet.Cells[1, 3] = "SalePrice";

//Format A1:C1 as Bold, Vertical Alingment = Center.
oSheet.get_Range("A1", "C1").VerticalAlignment = Excel_12.XlVAlign.xlVAlignCenter;


SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Server=192.168.10.113;Database=AmitTestData;User ID=sa;Password=sa;Trusted_Connection=False;";
DataSet ds=new DataSet();
Bussiness objBussiness = new Bussiness();
objBussiness.fncReturnDataTable(conn, "select * from BigScreenProducts", ref ds,"Table0");
objBussiness.fncReturnDataTable(conn, "select * from Employee", ref ds, "Table1");



// sqladp1.Fill(ds,"Table2");
int tblIndex = 1;
int AscciVal = 65; // Ascci Value of Capital 'A'
// Table Count
for (int tblNo = 0; tblNo < ds.Tables.Count; tblNo++)
{
// Creating Headings in the Excel
string[] arrcolumn = new string[ds.Tables[tblNo].Columns.Count];
int ColCnt = ds.Tables[tblNo].Columns.Count;

for (int colname=0; colname < ds.Tables[tblNo].Columns.Count; colname++)
{
arrcolumn[colname] = ds.Tables[tblNo].Columns[colname].ColumnName;
}
char start = (char)AscciVal;
char end = (char)(AscciVal + ColCnt-1);
AscciVal = AscciVal + ColCnt+1; // Value set to the next cell of the Excel sheet
int rowCnt = ds.Tables[tblNo].Rows.Count + 1; // Couting no of rows in the current table
oSheet.Cells[1, tblIndex] = "Table" + tblNo.ToString(); // Filling Table Name in the Table
tblIndex = tblIndex - 1;
tblIndex = tblIndex + ColCnt + 2;
oSheet.get_Range(start + "2", end + "2").Value2 = arrcolumn;
oSheet.get_Range(start + "2", end + "2").Font.Bold = true;

string[,] arr1 = new string[rowCnt - 1, ColCnt];
for (int i = 0; i < ds.Tables[tblNo].Rows.Count; i++)
{
for (int j = 0; j < ds.Tables[tblNo].Columns.Count; j++)
{
arr1[i, j] = ds.Tables[tblNo].Rows[i].ItemArray[j].ToString();
}
}

oSheet.get_Range(start + "3", end + rowCnt.ToString()).Value2 = arr1;
}
oXL.Visible = true;
oXL.UserControl = true;
}
catch (Exception theException)
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat(errorMessage, theException.Message);
errorMessage = String.Concat(errorMessage, " Line: ");
errorMessage = String.Concat(errorMessage, theException.Source);
MessageBox.Show(errorMessage, "Error");
}
}

private void button2_Click(object sender, EventArgs e)
{

}

private void btnStop_Click(object sender, EventArgs e)
{
this.Close();
}
// Function for Manupulate a variable no of columns
//private void DisplayQuarterlySales(Excel_12 oWS)
//{
// Excel_12._Workbook oWB;
// Excel_12.Series oSeries;
// Excel_12.Range oRecizeRange;
// Excel_12._Chart oChart;
// String sMsg;
// int iNumQtrs;
// // Display How many Quaters to Display Data for.
// for(
}

}





------------------------------------------
namespace WindowsExcelSheet
{
class Bussiness
{

public Bussiness()
{ }
public void fncReturnDataTable(SqlConnection conn, string strQuertString, ref DataSet ds, string strTabName)
{
SqlCommand cmd=null;
SqlDataAdapter sda=null;
try
{
cmd = new SqlCommand();
cmd.CommandText = strQuertString;
cmd.Connection = conn;
cmd.Connection.Open();
sda = new SqlDataAdapter(cmd);
sda.Fill(ds,strTabName);
}
catch (Exception Exp)
{

}
finally
{
cmd.Connection.Close();
cmd.Dispose();
cmd.Dispose(); sda.Dispose();
}
}
}
}
GeneralRe: Microsoft.Office.Interop.Excel.Application issue Pin
AndieDu21-May-09 0:08
AndieDu21-May-09 0:08 
AnswerRe: Microsoft.Office.Interop.Excel.Application issue Pin
AndieDu21-May-09 17:28
AndieDu21-May-09 17:28 
AnswerRe: Microsoft.Office.Interop.Excel.Application issue Pin
AmmySA22-May-09 5:39
AmmySA22-May-09 5:39 
GeneralRe: Microsoft.Office.Interop.Excel.Application issue Pin
AndieDu22-May-09 16:14
AndieDu22-May-09 16:14 
QuestionC# calling unmanaged C/C++ COM method with C++ reference type Pin
Klempie20-May-09 20:44
Klempie20-May-09 20:44 
AnswerRe: C# calling unmanaged C/C++ COM method with C++ reference type Pin
Klempie21-May-09 2:54
Klempie21-May-09 2:54 
GeneralRe: C# calling unmanaged C/C++ COM method with C++ reference type Pin
Klempie21-May-09 3:50
Klempie21-May-09 3:50 
QuestionSystem font scripts Pin
fungweizz20-May-09 20:38
fungweizz20-May-09 20:38 
AnswerRe: System font scripts Pin
Baran M20-May-09 21:00
Baran M20-May-09 21:00 
GeneralRe: System font scripts Pin
fungweizz20-May-09 21:55
fungweizz20-May-09 21:55 
QuestionVery urgent please Pin
Naveed72720-May-09 20:28
Naveed72720-May-09 20:28 
AnswerRe: Very urgent please Pin
Mycroft Holmes20-May-09 21:22
professionalMycroft Holmes20-May-09 21:22 
GeneralRe: Very urgent please Pin
Naveed72720-May-09 21:30
Naveed72720-May-09 21:30 
GeneralRe: Very urgent please Pin
Mycroft Holmes20-May-09 21:45
professionalMycroft Holmes20-May-09 21:45 
AnswerRe: Very urgent please Pin
MumbleB20-May-09 21:48
MumbleB20-May-09 21:48 
GeneralRe: Very urgent please Pin
Naveed72721-May-09 0:08
Naveed72721-May-09 0:08 
QuestionHow to pass values to managed to unmanaged code? Pin
mutpan20-May-09 19:31
mutpan20-May-09 19:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.