Winform将DataGridView导出Excel,调用打印机打印DataGridView

本文介绍了如何在Winform应用中将DataGridView数据导出到Excel文件,并详细讲解了如何实现对DataGridView的打印功能,包括创建打印类的步骤。
摘要由CSDN通过智能技术生成

DataGridView导出Excel

(这个需要 Microsoft.Office.Interop.Excel.dll  在网上下载就可以)

public static void ExportExcel(string fileName, DataGridView myDGV)
        {
            string saveFileName = "";
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.DefaultExt = "xls";
            saveDialog.Filter = "Excel文件|*.xls";
            saveDialog.FileName = fileName;
            saveDialog.ShowDialog();
            saveFileName = saveDialog.FileName;
            if (saveFileName.IndexOf(":") < 0) return; //被点了取消
            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            if (xlApp == null)
            {
                MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel");
                return;
            }
            Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
            Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
            Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
            //写入标题
            for (int i = 0; i < myDGV.ColumnCount; i++)
            {
                worksheet.Cells[1, i + 1] = myDGV.Columns[i].HeaderText;
            }
            //写入数值
            for (int r = 0; r < myDGV.Rows.Count; r++)
            {
                for (int i = 0; i < myDGV.ColumnCount; i++)
                {
                    worksheet.Cells[r + 2, i + 1] = myDGV.Rows[r].Cells[i].Value;
                }
                System.Windows.Forms.Application.DoEvents();
            }
            worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
            if (saveFileName != "")
            {
                try
                {
                    workbook.Saved = true;
                    workbook.SaveCopyAs(saveFileName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);
                }
            }
            xlApp.Quit();
            GC.Collect();//强行销毁
            MessageBox.Show("文件: " + fileName + ".xls 保存成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }


打印机打印DataGridView


1. 首先创建打印类

using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Data;
using System.Windows.Forms;


namespace WCY_LAB { 
public class GridPrinter
{
    // the grid to print
    private DataGridView dataGridView;
    // the PrintDocument
    private PrintDocument printDocument;
    // center printout?
    private bool centerOnPage;
    // has a title?
    private bool hasTitle;
    // title
    private string title;
    // font
    private Font titleFont;
    // title color
    private Color titleColor;
    // use paging?
    private bool paging;
    // row printing
    static int currentRow;
    // page printing
    static int pageNumber;
    // page width
    private int pageWidth;


    // page height
    private int pageHeight;
    // left margin
    private int leftMargin;
    // top margin
    private int topMargin;
    // right margin
    private int rightMargin;
    // bottom margin
    private int bottomMargin;
    // y location placeholder
    private float currentY;
    // grid sizes
    private float rowHeaderHeight;
    private List<float> rowsHeight;
    private List<float> columnsWidth;
    private float dataGridViewWidth;


    // c
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值