2014-03-07 86 views
0

我所擁有的是一個簡單的gridview在C#窗體窗體應用程序正在由數據庫填充。現在我想添加一個功能來打印它。我搜索並嘗試了不同的方法,但目前爲止沒有任何工作(一些打印的空白頁面,而另一些打印了錯誤的數據)。如何將DataGridView導出爲PDFth/Excel/Word以便輕鬆打印?

這是給我的最好的結果代碼(它不會打印可通過滾動查看的所有記錄)..

private void button1_Click(object sender, EventArgs e) 
    { 
     PrintDialog printDialog = new PrintDialog(); 
     printDialog.Document = printDocument1; 
     printDialog.UseEXDialog = true; 
     if (DialogResult.OK == printDialog.ShowDialog()) 
     { 
      printDocument1.DocumentName = "Test Page Print"; 
      printDocument1.Print(); 
     } 
    } 

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) 
    { 
     Bitmap bm = new Bitmap(this.dgRecords.Width, this.dgRecords.Height); 
     this.dgRecords.DrawToBitmap(bm, new Rectangle(0, 0, this.dgRecords.Width, this.dgRecords.Height)); 
     e.Graphics.DrawImage(bm, 0, 0); 
    } 

現在我想也許變通是,如果我可以將datagridview導出到任何其他文件,例如excel表格,pdf或word文檔,然後我可以從該文件中打印出來。我試圖將其導出爲ex​​cel,但尚未成功。它只是打印標題和一行。

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

代碼能有人告訴我我在做什麼錯?我只需要打印gridview - 不管它是直接通過打印按鈕還是通過將其導出到其他文件。

編輯:

 Microsoft.Office.Interop.Excel.Application ExcelApp = 
       new Microsoft.Office.Interop.Excel.Application(); 
     Microsoft.Office.Interop.Excel._Workbook ExcelBook; 
     Microsoft.Office.Interop.Excel._Worksheet ExcelSheet; 

     int i = 0; 
     int j = 0; 

     //create object of excel 
     ExcelBook = (Microsoft.Office.Interop.Excel._Workbook)ExcelApp.Workbooks.Add(1); 
     ExcelSheet = (Microsoft.Office.Interop.Excel._Worksheet)ExcelBook.ActiveSheet; 
     //export header 
     for (i = 1; i <= this.dataGridView1.Columns.Count; i++) 
     { 
      ExcelSheet.Cells[1, i] = this.dataGridView1.Columns[i - 1].HeaderText; 
     } 

     //export data 
     for (i = 1; i <= this.dataGridView1.RowCount; i++) 
     { 
      for (j = 1; j <= dataGridView1.Columns.Count; j++) 
      { 
       ExcelSheet.Cells[i + 1, j] = dataGridView1.Rows[i - 1].Cells[j - 1].Value; 
      } 
     } 

     ExcelApp.Visible = true; 

     //set font Khmer OS System to data range 
      Microsoft.Office.Interop.Excel.Range myRange = ExcelSheet.get_Range(
       ExcelSheet.Cells[1, 1], 
       ExcelSheet.Cells[this.dataGridView1.RowCount + 1, 
        this.dataGridView1.Columns.Count]); 

     Microsoft.Office.Interop.Excel.Font x = myRange.Font; 
     x.Name = "Arial"; 
     x.Size = 10; 

     //set bold font to column header 
     myRange = ExcelSheet.get_Range(ExcelSheet.Cells[1, 1], 
           ExcelSheet.Cells[1, this.dataGridView1.Columns.Count]); 
     x = myRange.Font; 
     x.Bold = true; 
     //autofit all columns 
     myRange.EntireColumn.AutoFit(); 

     // 
     ExcelSheet = null; 
     ExcelBook = null; 
     ExcelApp = null; 

好了,所以我嘗試出口的這種方法的超越和它工作得很好,它開闢了一個Excel工作表從我的DataGrid,但開放的Excel工作表後,它的所有數據在這條線產生RuntimeBinder異常

 '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]); 

有什麼事是問題嗎?我想不出來...

回答

0

DataGridView不支持直接打印功能。您已開始正確使用PrintDocument並處理PrintPage事件。

在打印頁面事件處理程序中,您必須逐行打印DataGridView。有關更多信息,請參閱此article

示例代碼:

上課水平變量int rowIndex=0;

PrintPage event handler

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; 
+0

謝謝你的答案。我的VS似乎無法解析GridRow符號。這是爲什麼? – NewbieProgrammer

+0

@ user3331470 - 我已更正了代碼。代碼就是一個例子。您仍然繪製邊框,標題文本等...請參閱我已鏈接的文章。它爲您提供了一個完整的工作示例 – Junaith

相關問題