我刚刚接收winform 还是个新人,才能有限阿
现在要在winform中先取得gridview类似控件中的所选主键后再到表中将该主键对应行的所有数据以WORD形式打印
也就是:
gridview的行主键为试卷名称(ex_n),取得ex_n后到试卷表(ex_p)中打印该试卷哪位大侠做过类似的工作阿?
Help 阿!@
现在要在winform中先取得gridview类似控件中的所选主键后再到表中将该主键对应行的所有数据以WORD形式打印
也就是:
gridview的行主键为试卷名称(ex_n),取得ex_n后到试卷表(ex_p)中打印该试卷哪位大侠做过类似的工作阿?
Help 阿!@
解决方案 »
- BackGroundWorker 组件 内存释放问题
- winform可以 wcf不可以?
- C#计算器怎么做
- 请求帮助,数字算法问题。请看下面的一组数字。在线~~~~~~~
- 如何控制窗体的显示与隐藏?
- 数据库的错误异常,可以被c#读取并识别么?
- winform窗体设置FormBorderStyle属性None后无法在关闭了
- web form 如何对GridView进行空绑定(显示标题兰和边框,数据就不用显示了)?
- C#中调用DTS包时出现连接不上数据库得错误并且出现能读处DTS包版本号,但是找不到文件!请指教啊!!
- c#怎样关闭浏览器窗口?
- 【C#】怎样到远程服务器上查询,增加,修改xml文件的内容,急,急,急
- 请问高手:这个图片切换效果为什么在html文件中能显示正常,在c#中就不正常了呢?
你是要打印数据吧,怎么取数据我就不说了,你取得一个试卷表(ex_p)的数据存在Datatable中,然后打印该table中的数据,打印的方法很多。建议使用Printing类来自己实现打印,这样格式可以自己订,很方便。
就是用Printing和Drawing一行一行的绘制。附部分代码,lz可以网上找找,很多的using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Drawing.Printing;namespace Rstr_Fmg
{
public class PrintToDGHelp
{ StringFormat strFormat; //Used to format the grid rows.
ArrayList arrColumnLefts = new ArrayList();//Used to save left coordinates of columns
ArrayList arrColumnWidths = new ArrayList();//Used to save column widths
int iCellHeight = 0; //Used to get/set the datagridview cell height
int iTotalWidth = 0; //
int iRow = 0;//Used as counter
bool bFirstPage = false; //Used to check whether we are printing first page
bool bNewPage = false;// Used to check whether we are printing a new page
int iHeaderHeight = 0; //Used for the header height
private int mCurrentPageIndex = 0;
PrintDocument printDocument1 = new PrintDocument(); DataGridView _dataGridView1 = new DataGridView();
private string _Title;
private string _Caption;
private string[,] _Heard;
public string StarHelp(string Title, string Caption, DataGridView DataSource, string[,] Heard)
{ _dataGridView1 = DataSource;
_Title = Title;
_Heard = Heard;
_Caption = Caption;
SetPrintDialog(_dataGridView1);
return "true";
} private void SetPrintDialog(DataGridView dataGridView1)
{
PrintDialog printDialog = new PrintDialog();
printDialog.Document = printDocument1;
printDialog.UseEXDialog = true; //Get the document
if (DialogResult.OK == printDialog.ShowDialog())
{
printDocument1.DocumentName = "Test Page Print";
printDocument1.BeginPrint += PrintHelpBeginPrint;
printDocument1.PrintPage += PrintHelpPrintPage;
PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
printPreviewDialog.ClientSize = new Size(400, 300);
printPreviewDialog.Location = new Point(29, 29);
printPreviewDialog.Name = "Print Preview Dialog";
printPreviewDialog.UseAntiAlias = true;
printPreviewDialog.Document = printDocument1;
printPreviewDialog.ShowDialog();
}
} private void PrintHelpBeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
try
{
strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Near;
strFormat.LineAlignment = StringAlignment.Center;
strFormat.Trimming = StringTrimming.EllipsisCharacter; arrColumnLefts.Clear();
arrColumnWidths.Clear();
iCellHeight = 0;
iRow = 0;
bFirstPage = true;
bNewPage = true; // Calculating Total Widths
iTotalWidth = 0;
foreach (DataGridViewColumn dgvGridCol in _dataGridView1.Columns)
{
iTotalWidth += dgvGridCol.Width;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} private void PrintHelpPrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{ StarPrintPage(e, _Title, _Caption, _dataGridView1, _Heard);
} private void StarPrintPage(PrintPageEventArgs e, string Title, string Caption, DataGridView dataGridView1, string[,] Heard)
{
try
{
我这里有一个把GridView中数据用Excel显示打印的类,如果把Excel换成Word,Word的对象模型可能参考
http://msdn.microsoft.com/zh-cn/library/kw65a0we(VS.80).aspx
楼主可以参考一下,看看有没有用。using System;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Excel;
using System.Windows.Forms; //此类只为解决,DataGrid表用Excel打印的问题
public class printDataGridView
{
public printDataGridView(string tblHeader)
{
_TableHead = tblHeader; //显示打印表头名称
}
private string _TableHead;
public void btnPrint_Click(DataGridView curDataGridView, bool blnPrint)
{
//curDataGridView 为数据源
//blnPrint 为参数,True时直接显示打印预览,False时保存为Excel文件 DataGridView heheGo;
heheGo = curDataGridView;
Microsoft.Office.Interop.Excel.ApplicationClass curExcel = new Microsoft.Office.Interop.Excel.ApplicationClass(); if (heheGo != null)
{
try
{
//System.Data.DataTable tempView = ((DataView)heheGo.DataSource).ToTable();
Workbook curWorkBook = curExcel.Workbooks.Add(true);
Worksheet curSheet = (Worksheet)curWorkBook.ActiveSheet;
curSheet.Name = "平凡软件"; curSheet.Cells[1, 2] = _TableHead;
curWorkBook.Saved = true;
//数据赋值
int i = 1;
int j = 2;
//表头
foreach (DataGridViewColumn curColumn in heheGo.Columns)
{
if (curColumn.Visible)
{
curSheet.Cells[j, i] = curColumn.HeaderText; //设定列宽
Range curRange = curSheet.get_Range(curSheet.Cells[j, i], curSheet.Cells[j, i]);
curRange.ColumnWidth = curColumn.Width / 7; i++;
} } //输出数据 j++;
foreach (DataGridViewRow curRow in heheGo.Rows)
{
i = 1;
foreach (DataGridViewColumn curColumn in heheGo.Columns)
{
if (curColumn.Visible)
{
if (curRow.Cells[curColumn.Name].Value != null && curRow.Cells[curColumn.Name].Value != Convert.DBNull)
{
curSheet.Cells[j, i] = curRow.Cells[curColumn.Name].FormattedValue.ToString();
}
else
{
curSheet.Cells[j, i] = "";
}
i++;
}
}
j++;
} //表格格式调整
curSheet.PageSetup.PrintGridlines = true;
curSheet.PageSetup.PaperSize = XlPaperSize.xlPaperA4;
curSheet.PageSetup.Orientation = XlPageOrientation.xlLandscape;
curSheet.PageSetup.PrintTitleRows = "A1:AH2";
curSheet.PageSetup.LeftMargin = 30;
curSheet.PageSetup.RightMargin = 30;
curSheet.PageSetup.TopMargin = 30;
curSheet.PageSetup.BottomMargin = 30;
//curSheet.PageSetup.Zoom = 100; curExcel.DisplayAlerts = false;
if (blnPrint)
{
curExcel.Visible = true; curSheet.PrintPreview(true);
}
else
{
SaveFileDialog jbs = new SaveFileDialog();
jbs.Filter = "数据表格文件 (*.xls)|*.xls";
jbs.Title = "保存配方文件";
if (jbs.ShowDialog() == DialogResult.OK)
{ curWorkBook.SaveCopyAs(jbs.FileName);
}
}
}
catch (Exception E)
{
MessageBox.Show("导出文件错误!" + E.Message); }
finally
{
curExcel.Quit(); } }
} }