C# 如何将DataGridView导出到PDFth/Excel/Word以便于打印?
伙计们,我有一个C#windows窗体应用程序中的简单gridview,它由一个数据库填充。现在我想添加一个功能来打印它。我在谷歌上搜索并尝试了不同的方法,但到目前为止都没有成功(一些打印的是空白页,而另一些打印的是错误的数据) 这是给我最好结果的代码(它不会打印所有可以通过滚动查看的记录) 现在我想,如果我可以将datagridview导出到任何其他文件,如excel工作表、pdf或word文档,然后可以从该文件打印出来,也许可以解决这个问题。我尝试将其导出到excel,但没有成功。它只打印标题和一行 这是excel导出的代码C# 如何将DataGridView导出到PDFth/Excel/Word以便于打印?,c#,printing,datagridview,C#,Printing,Datagridview,伙计们,我有一个C#windows窗体应用程序中的简单gridview,它由一个数据库填充。现在我想添加一个功能来打印它。我在谷歌上搜索并尝试了不同的方法,但到目前为止都没有成功(一些打印的是空白页,而另一些打印的是错误的数据) 这是给我最好结果的代码(它不会打印所有可以通过滚动查看的记录) 现在我想,如果我可以将datagridview导出到任何其他文件,如excel工作表、pdf或word文档,然后可以从该文件打印出来,也许可以解决这个问题。我尝试将其导出到excel,但没有成功。它只打印标
private void ToCsV(DataGridView dGV, string filename)
{
string stOutput = "";
string sHeaders = "";
for (int j = 0; j < dGV.Columns.Count; j++)
sHeaders = sHeaders.ToString() + Convert.ToString(dGV.Columns[j].HeaderText) + "\t";
stOutput += sHeaders + "\r\n";
for (int i = 0; i < dGV.RowCount - 1; i++)
{
string stLine = "";
for (int j = 0; j < dGV.Rows[i].Cells.Count; j++)
stLine = stLine.ToString() + Convert.ToString(dGV.Rows[i].Cells[j].Value) + "\t";
stOutput += stLine + "\r\n";
}
Encoding utf16 = Encoding.GetEncoding(1254);
byte[] output = utf16.GetBytes(stOutput);
FileStream fs = new FileStream(filename, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(output, 0, output.Length); //write the encoded file
bw.Flush();
bw.Close();
fs.Close();
}
private void button3_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Excel Documents (*.xls)|*.xls";
sfd.FileName = "export.xls";
if (sfd.ShowDialog() == DialogResult.OK)
{
ToCsV(dgRecords, sfd.FileName);
}
}
在这条线上
Microsoft.Office.Interop.Excel.Range myRange = ExcelSheet.get_Range(
ExcelSheet.Cells[1, 1],
ExcelSheet.Cells[this.dataGridView1.RowCount + 1,
this.dataGridView1.Columns.Count]);
有什么问题吗?我想不出来…
DataGridView
不支持直接打印功能。使用PrintDocument
和处理PrintPage
事件的方向是正确的
在打印页面事件处理程序中,必须逐行打印DataGridView
。有关更多信息,请参见此
示例代码:
类级变量introwIndex=0代码>
在打印页面事件处理程序中
int rowTop=e.MarginBounds.Top;
bool needMorePages=false;
while (rowIndex<= dataGridView1.Rows.Count - 1)
{
DataGridViewRow row = dataGridView1.Rows[rowIndex];
if(rowTop + row.Height >= e.MarginBounds.Top + e.MarginBounds.Height)
{
needMorePages = true;
break;
}
foreach (DataGridViewCell Cell in row.Cells)
{
//draw cell content
}
rowTop += row.Height;
rowIndex++;
}
e.HasMorePages = needMorePages;
int rowTop=e.MarginBounds.Top;
bool needMorePages=false;
而(行索引=e.MarginBounds.Top+e.MarginBounds.Height)
{
needMorePages=true;
打破
}
foreach(row.Cells中的DataGridViewCell单元格)
{
//绘制单元格内容
}
rowTop+=行高;
rowIndex++;
}
e、 HasMorePages=需要更多页面;
谢谢您的回答。我的VS似乎无法解析网格符号。为什么?@user3331470-我已经更正了代码。代码就是一个例子。您仍然绘制边框、标题文本等。。。请看我链接的文章。它为您提供了一个完整的工作示例
'object' does not contain a definition for 'get_Range'
Microsoft.Office.Interop.Excel.Range myRange = ExcelSheet.get_Range(
ExcelSheet.Cells[1, 1],
ExcelSheet.Cells[this.dataGridView1.RowCount + 1,
this.dataGridView1.Columns.Count]);
int rowTop=e.MarginBounds.Top;
bool needMorePages=false;
while (rowIndex<= dataGridView1.Rows.Count - 1)
{
DataGridViewRow row = dataGridView1.Rows[rowIndex];
if(rowTop + row.Height >= e.MarginBounds.Top + e.MarginBounds.Height)
{
needMorePages = true;
break;
}
foreach (DataGridViewCell Cell in row.Cells)
{
//draw cell content
}
rowTop += row.Height;
rowIndex++;
}
e.HasMorePages = needMorePages;