使用C#打印网格视图数据 [英] print gird view data using c#

查看:105
本文介绍了使用C#打印网格视图数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我已经在网格中显示了数据类别和学生人数,
我想添加一个按钮,单击该按钮时,我想通过打印机从该数据中取出打印内容,

我已经看到许多将这些数据转换为图像的代码,但是我想要的数据就像在excell中打印的一样,包括单元格边框.

谁能帮助我

解决方案

我不是专家,但是...使用Crystal Report或Microsoft Report Viewer可能会帮助您.
查看此链接以了解报告查看器
它具有用于打印的内置控件

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>


嗨. .
您可以使用以下C#代码将任何SQL表数据(此处为班级和学生人数)导出到excel工作表中.在此之前,您必须添加MICROSOFT OFFICE EXCEL 12.0的COM参考. /> 这是用于将sql数据导出到excel的常规代码.

使用系统;
使用System.Configuration;
使用System.Data;
使用System.Linq;
使用System.Web;
使用System.Web.Security;
使用System.Web.UI;
使用System.Web.UI.HtmlControls;
使用System.Web.UI.WebControls;
使用System.Web.UI.WebControls.WebParts;
使用System.Xml.Linq;
使用System.Data.Sql;
使用System.Data.SqlClient;
使用Microsoft.Office.Interop.Excel;

公共局部类_Default:System.Web.UI.Page
{
受保护的void Page_Load(对象发送者,EventArgs e)
{


}

//在您的Button单击事件中

受保护的void Button1_Click(对象发送者,EventArgs e)
{
试试
{
字符串constr =您的连接字符串";

SqlConnection conn =新的SqlConnection(constr);
conn.Open();
SqlCommand命令=新的SqlCommand("select * from table_name",conn);
DataSet数据集= new DataSet();
SqlDataAdapter adapter =新的SqlDataAdapter(command);
adapter.Fill(dataset);
Microsoft.Office.Interop.Excel.ApplicationClass excel =新的ApplicationClass();
excel.Application.Workbooks.Add(true);
System.Data.DataTable表=数据集.表[0];
int ColumnIndex = 0;
foreach(table.Columns中的System.Data.DataColumn col)
{
ColumnIndex ++;
excel.Cells [1,ColumnIndex] = col.ColumnName;
}
int rowIndex = 0;
foreach(表中的DataRow行.行)
{
rowIndex ++;
ColumnIndex = 0;
foreach(table.Columns中的DataColumn col)
{
ColumnIndex ++;
excel.Cells [rowIndex + 1,ColumnIndex] = row [col.ColumnName];
}
}
excel.Visible = true;
工作表工作表=(工作表)excel.ActiveSheet;
worksheet.Activate();
}
catch(ex ex例外)
{
Response.Write(ex.Message);
}
}
}


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

解决方案

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>


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);
}
}
}


这篇关于使用C#打印网格视图数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆