Click here to Skip to main content
15,900,818 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i have displayed data class and number of students in a grid,
i want to add a button, when it is clicked, i want to take print out of that data by printer,

i have seen many codes which convert that data into image, but i want data just like printed in excell, including cell borders.

can anyone help me out
Posted

i am not a expert but...using crystal report or microsoft report viewer may help u..

check out this link to learn the report viewer
it has inbuilt control that is used for printing

http://www.packtpub.com/article/creating-report-with-visual-studio-2008
<a href="">http://www.packtpub.com/article/creating-report-with-visual-studio-2008</a>
 
Share this answer
 
v2
Hi...
You can export any of your SQL table data(here class and no. of students in ur case) into an excel sheet by using the following C# code.Before doing this, you have to add COM reference for MICROSOFT OFFICE EXCEL 12.0 .
This is a general code for exporting sql data to excel.

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Sql;
using System.Data.SqlClient;
using Microsoft.Office.Interop.Excel;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{


}

// In your Button click event

protected void Button1_Click(object sender, EventArgs e)
{
try
{
string constr = "YOUR CONNECTION STRING";

SqlConnection conn = new SqlConnection(constr);
conn.Open();
SqlCommand command = new SqlCommand("select * from table_name", conn);
DataSet dataset = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(command);
adapter.Fill(dataset);
Microsoft.Office.Interop.Excel.ApplicationClass excel = new ApplicationClass();
excel.Application.Workbooks.Add(true);
System.Data.DataTable table = dataset.Tables[0];
int ColumnIndex = 0;
foreach (System.Data.DataColumn col in table.Columns)
{
ColumnIndex++;
excel.Cells[1, ColumnIndex] = col.ColumnName;
}
int rowIndex = 0;
foreach (DataRow row in table.Rows)
{
rowIndex++;
ColumnIndex = 0;
foreach (DataColumn col in table.Columns)
{
ColumnIndex++;
excel.Cells[rowIndex + 1, ColumnIndex] = row[col.ColumnName];
}
}
excel.Visible = true;
Worksheet worksheet = (Worksheet)excel.ActiveSheet;
worksheet.Activate();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
 
Share this answer
 
Comments
Anil Honey 206 25-Aug-11 7:41am    
it will work
Db issues 27-Aug-11 1:36am    
Thanks,

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