精华内容
下载资源
问答
  • 上图就是打印的效果,但表格的样式没有出来,在vue-cli脚手架里样式是起作用的。 这个打印是用的原生javascript的方法实现的,但引用了vue.js和element 代码如下: <!DOCTYPE html> <html> <...

    第一种表格

    先上效果图:
    在这里插入图片描述
    在这里插入图片描述
    上图就是打印的效果,但表格的样式没有出来,在vue-cli脚手架里样式是起作用的。
    这个打印是用的原生javascript的方法实现的,但引用了vue.jselement

    代码如下:

      <!DOCTYPE html>
        <html>
        <head>
            <meta charset="utf-8" />
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <title>vue中打印</title>
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
            <style>
             #printTest table{
             font-family:"宋体";
             border-collapse:collapse;
             width:99.5%;
             }
             #printTest table thead th{
             height: 40px;
             font-size: 13px;
             /* width: 10px; */
             text-align: center;
             border: 1px solid black;
             }
             #printTest table tbody tr{
             font-size:13px;
             border: 1px solid black;
             height: 40px;
             text-align: center;
             }
             #printTest table tbody td{
             font-size: 13px;
             text-align: center;
             border: 1px solid black;
             }
            </style>
        </head>
        <body>
            <div id="apps">
                <el-button class="filter-item" @click="handleFinish" type="primary">打印</el-button>
                <el-dialog title="详情单" :close-on-click-modal="false" :visible.sync="printDialogVisible"
                    width="60%">
                    <el-button type="success"  @click="yinp">
                        <svg-icon icon-class="print" />打印</el-button>
                    <div id="printTest">
                        <div style="font-size: 22px;padding-bottom:10px;font-weight:800;font-family:宋体; text-align:center">表单
                        </div>
                        <span style="font-size:16px;font-family:宋体;float:left;">客户名称:</span>
                        <span style="font-size:16px;font-family:宋体;float:right;">日期:20190803</span>
                        <table>
                            <thead>
                                <th>日期</th>
                                <th>名字</th>
                                <th>地址</th>
                                <th>爱好</th>
                                <th>数量</th>
                                <th>运动</th>
                                <th>备注</th>
                            </thead>
                            <tbody>
                                <tr v-for="item in tableData" :key="item.id">
                                    <td>{{item.date}}</td>
                                    <td>{{item.name}}</td>
                                    <td>{{item.address}}</td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </el-dialog>
        
            </div>
        </body>
        <script src="https://cdn.bootcss.com/vue/2.5.1/vue.min.js"></script>
        <script src="https://cdn.bootcss.com/element-ui/2.3.9/index.js"></script>
        
        <script>
        var apps = new Vue({
            el:'#apps',
            data(){
                return{
                printDialogVisible:false,
                 tableData: [{
                 id:1,
                 date: '2016-05-02',
                 name: '王小虎',
                 address: '上海市普陀区金沙江路 1518 弄'
                 }, {
                 id:2,
                 date: '2016-05-04',
                 name: '王小虎',
                 address: '上海市普陀区金沙江路 1517 弄'
                 }, {
                 id:3,
                 date: '2016-05-01',
                 name: '王小虎',
                 address: '上海市普陀区金沙江路 1519 弄'
                 }, {
                 id:4,
                 date: '2016-05-03',
                 name: '王小虎',
                 address: '上海市普陀区金沙江路 1516 弄'
                 }]
                }
            },
          methods: {
              handleFinish(){
              this.printDialogVisible = true
              },
              yinp(){
                var newstr = document.getElementById("printTest").innerHTML;//得到需要打印的元素HTML
                var oldstr = document.body.innerHTML
                document.body.innerHTML = newstr;
                window.print();
                document.body.innerHTML = oldstr;
              },
          },
        })
        </script>
        </html>
    

    第二种表格
    在这里插入图片描述
    这个是vue-cli脚手架里用的打印功能。
    通过npm 安装插件

    1,安装 npm install vue-print-nb --save

    2,引入 安装好以后在main.js文件中引入

      import Print from 'vue-print-nb'
     Vue.use(Print);  //注册
    

    3,现在就可以使用了

      <div id="printTest" >
        
              <p>锄禾日当午</p>
        
              <p>汗滴禾下土 </p>
        
              <p>谁知盘中餐</p>
        
              <p>粒粒皆辛苦</p>
        
            </div>
        
            <button v-print="'#printTest'">打印</button>
    

    现在就把表格代码贴出来:

      <el-dialog title="设备厂内迁移单预览" v-dialogDrag :close-on-click-modal="false" :visible.sync="printDialogVisible" width="99.5%">
                <el-button type="primary" @click="printDialogVisible = false">返回</el-button>
                <el-button type="success" v-print="'#printTest2'"><svg-icon icon-class="print"/>打印</el-button>
        				<div id="printTest2">
        					<table border= "0" width="99.5%">
                  <thead>
                           <tr>
                             <th colspan="12">{{daolist.transferInCompanyName}}</th>
                           </tr>
                           <tr>
                              <th colspan="12">设备厂内迁移单</th>
                           </tr>
                           <tr>
                             <td colspan="12"><span>调出单位:{{daolist.transferOutCompanyName}} / {{daolist.transferOutFactoryName}} / {{daolist.transferOutDepartmentName}}</span></td>
                           </tr>
                            <tr>
                             <td colspan="6"><span>调入单位:{{daolist.transferInCompanyName}} / {{daolist.transferInFactoryName}} / {{daolist.transferInDepartmentName}}</span></td>
                             <td colspan="6" style="text-align:right"><span>编号:{{daolist.formCode}}</span></td>
                           </tr>
        
                     </thead>
        
                    <tbody>
                      <tr >
                        <td rowspan="2" style="text-align:center;border:1px solid black;">固定资产编号</td>
                        <td rowspan="2" style="text-align:center;border:1px solid black;">设备名称</td>
                        <td rowspan="2" style="text-align:center;border:1px solid black;">型号规格</td>
                        <td rowspan="2" style="text-align:center;border:1px solid black;">出厂编号</td>
                        <td rowspan="2" style="text-align:center;border:1px solid black;">制造厂家</td>
                        <td rowspan="2" style="text-align:center;border:1px solid black;">数量</td>
        
                        <td rowspan="2" style="text-align:center;border:1px solid black;">单位</td>
                        <td colspan="2" style="text-align:center;border:1px solid black;">年限</td>
                        <td colspan="2" style="text-align:center;border:1px solid black;">固定资产价值(元)</td>
                        <td rowspan="2" style="text-align:center;border:1px solid black;">备注</td>
                        
                      </tr>
                      <tr>
                        <td style="text-align:center;border:1px solid black;">折旧</td>
                        <td style="text-align:center;border:1px solid black;">已用</td>
                        <td style="text-align:center;border:1px solid black;">原值</td>
                        <td style="text-align:center;border:1px solid black;">净值</td>
                      </tr>
        
                     
                       <tr style="height:50px">
                        <td style="text-align:center;border:1px solid black;">{{daolist.fixedAssetCode}}</td>
                        <td style="text-align:center;border:1px solid black;">{{daolist.equipName}}</td>
                        <td style="text-align:center;border:1px solid black;">{{daolist.equipType}}</td>
                        <td style="text-align:center;border:1px solid black;">{{daolist.serialNumber}}</td>
                        <td style="text-align:center;border:1px solid black;">{{daolist.manufacturer}}</td>
                        <td style="text-align:center;border:1px solid black;">{{daolist.nums}}</td>
        
                        <td style="text-align:center;border:1px solid black;"></td>
                        <td style="text-align:center;border:1px solid black;">{{daolist.depreciationPeriod}}</td>
                        <td style="text-align:center;border:1px solid black;">{{daolist.usedPeriod}}</td>
                        <td style="text-align:center;border:1px solid black;">{{daolist.originalFinancialValue}}</td>
                        <td style="text-align:center;border:1px solid black;">{{daolist.netBookValue}}</td>
                        <td style="text-align:center;border:1px solid black;">{{daolist.remark}}</td>
                      
                      </tr>
                   <tr style="height:70px">
                     <td colspan="1" style="border:1px solid black">调拨原因</td>
                     <td colspan="11" style="text-align:left;border:1px solid black">{{daolist.reason}}</td>
                   </tr>
                    <tr style="height:70px">
                     <td colspan="1" style="border:1px solid black">其他说明</td>
                     <td colspan="11" style="text-align:left;border:1px solid black">{{daolist.otherDescription}}</td>
                   </tr>
                   <tr style="height:50px">
                     <td colspan="2" style="border:1px solid black">经办人签字</td>
                     <td colspan="3" style="border:1px solid black">设备科长签字</td>
                     <!-- <td colspan="2">调出厂签字</td> -->
                     <td colspan="2" style="border:1px solid black">设备副总签字</td>
                     <td colspan="3" style="border:1px solid black">资产部副部长签字</td>
                     <td colspan="2" style="border:1px solid black">资产部盖章</td>
                   </tr>   
                   <tr style="height:50px">
                     <td colspan="2" style="border:1px solid black;border-bottom:none" align=“left” valign="top"><div style="width:265px">意见:{{operator}}</div></td>
                     <td colspan="3" style="border:1px solid black;border-bottom:none" align=“left” valign="top"><div style="width:265px">意见:{{branch}}</div></td>
                     <!-- <td colspan="2" align=“left” valign="top">意见:</td> -->
                     <td colspan="2" style="border:1px solid black;border-bottom:none" align=“left” valign="top"><div style="width:205px">意见:{{fuzong}}</div></td>
                     <td colspan="3" style="border:1px solid black;border-bottom:none" align=“left” valign="top"><div style="width:265px">意见:{{zichan}}</div></td>
                     <td colspan="2" style="border:1px solid black;border-bottom:none" align=“left” valign="top"><div style="width:265px">意见:</div></td>
                   </tr>
                     <tr style="height:40px">
                     <td colspan="2" style="border:1px solid black;border-bottom:none;border-top:none">经办人:{{operatorname}}</td>
                     <td colspan="3" style="border:1px solid black;border-bottom:none;border-top:none">经办人:{{branchname}}</td>
                     <!-- <td colspan="2" style="border-top:1px solid #fff">经办人:</td> -->
                     <td colspan="2" style="border:1px solid black;border-bottom:none;border-top:none">经办人:{{fuzongname}}</td>
                     <td colspan="3" style="border:1px solid black;border-bottom:none;border-top:none">经办人:{{zichanname}}</td>
                     <td colspan="2" style="border:1px solid black;border-bottom:none;border-top:none">经办人:</td>
                   </tr>
                   <tr style="height:40px">
                     <td colspan="2" style="border:1px solid black;border-top:none">日期:{{operatordate}}</td>
                     <td colspan="3" style="border:1px solid black;border-top:none">日期:{{branchdate}}</td>
                     <!-- <td colspan="2" style="border-top:1px solid #fff">日期:</td> -->
                     <td colspan="2" style="border:1px solid black;border-top:none">日期:{{fuzongdate}}</td>
                     <td colspan="3" style="border:1px solid black;border-top:none">日期:{{zichandate}}</td>
                     <td colspan="2" style="border:1px solid black;border-top:none">日期:</td>
                     </tr>
                    </tbody>
                  </table>
        				</div>
        			</el-dialog>
    

    这个是一个弹框里的内容。
    css代码:

      #printTest2 table{
          border-collapse:collapse;
        }
        #printTest2 table thead th{
          font-size: 20px;
          padding:10px;
        }
        #printTest2 table tbody tr{
          height:30px;
          font-size:14px;
        }
    
        #printTest2 table tbody td span{
          margin-right:20px;
        }
    

    第三种表格的写法在这里插入图片描述
    这个几种表格的格式是不一样的:

    代码:

      <el-dialog title="设备报修单预览" v-dialogDrag :close-on-click-modal="false" :visible.sync="printDialogVisible" width="60%">
                <el-button type="primary" @click="printDialogVisible = false">返回</el-button>
                <el-button type="success" v-print="'#printTest1'"><svg-icon icon-class="print"/>打印</el-button>
        				<div id="printTest1">
                   <!-- <h2 style="text-align:center;">{{srintList.repairCompany}}设备报修单</h2> -->
                  
        					<table border= "0" width="99%" >
                    <thead>
                           <tr>
                               <th colspan="8">{{srintList.repairCompany}}设备报修单</th>
                           </tr>
                    </thead>
                    <tbody>
                      <tr style="height:60px">
                        <td style="border:1px solid black">设备名称</td>
                        <td style="border:1px solid black" colspan="3">{{srintList.equipmentName}}</td>
                        <td style="border:1px solid black" colspan="3">固定资产编号</td>
                        <td style="border:1px solid black" colspan="1">{{srintList.fixedAssetCode}}</td>
                      </tr>
                     
                      <tr style="height:60px">
                        <td style="border:1px solid black">型号规格</td>
                        <td style="border:1px solid black" colspan="3">{{srintList.model}}</td>
                        <td style="border:1px solid black" colspan="3">报修时间</td>
                        <td style="border:1px solid black" colspan="1">{{srintList.repariDate}}</td>
                      </tr>
                       <tr style="height:60px">
                        <td style="border:1px solid black">电路板名称</td>
                        <td style="border:1px solid black" colspan="3">{{srintList.circuitBoardName}}</td>
                        <td style="border:1px solid black" colspan="3">电路板型号</td>
                        <td style="border:1px solid black" colspan="1">{{srintList.circuitBoardModel}}</td>
                      </tr >
                       <tr style="height:60px">
                        <td style="border:1px solid black">报修部门</td>
                        <td style="border:1px solid black" colspan="3">{{srintList.repairDivision}}</td>
                        <td style="border:1px solid black" colspan="3">要求完成时间</td>
                        <td style="border:1px solid black" colspan="1">{{srintList.requestDate}}</td>
                      </tr>
                      <tr>
                       <td colspan="8" style="height:40px;border:1px solid black">
                        <!-- <input type="checkbox" id="cheni1"><span>外部维修</span>
                        <input type="checkbox" id="cheni2"><span>内部维修</span> -->
                         <span style="margin-left:170px">外部维修(<span v-if="suggestShow"></span>)</span><span style="margin-left:120px">内部维修(<span v-if="sugg"></span>)</span>
                       </td>
                     </tr>
                      <tr style="height:100px">
                        <td colspan="8" style="border:1px solid black" align="left" valign="top">
                          <div>故障原因及内容:{{srintList.causeReason}}</div>
                          </td>
                      </tr>
                    
                     
                      <tr style="height:100px">
                        <td colspan="8" align="left" valign="top" style="height:50px;border:1px solid black;border-bottom:none">设备科审核意见:{{fixing}}</td>
                      </tr>
                      <tr style="border-top:0px solid white;">    
                        <td colspan="8" align="right" style="border:1px solid black;border-top:none"><span>签字:{{fixingName}}</span><span>日期:{{fixingDate}}</span></td>
                      </tr>
                      <tr style="height:100px">
                        <td colspan="8" align="left" valign="top" style="height:50px;border:1px solid black;border-bottom:none">厂长审核意见:{{factoryst}}</td>
                      </tr>
                      <tr style="border-top:0px solid white;"> 
                        <td colspan="8" align="right" style="border:1px solid black;border-top:none"><span>签字:{{factorystName}}</span><span>日期:{{factorystDate}}</span></td>
                      </tr>
                       <tr style="height:100px">
                        <td colspan="8" align="left" valign="top" style="height:50px;border:1px solid black;border-bottom:none">事业部分管副总意见:{{enterprise}}</td>
                      </tr>
                      <tr style="border-top:0px solid white;">
                        <td colspan="8" align="right" style="border:1px solid black;border-top:none"><span>签字:{{enterpriseName}}</span><span>日期:{{enterpriseDate}}</span></td>
                      </tr>
                      <tr style="height:100px">
                        <td colspan="8" align="left" valign="top" style="height:50px;border:1px solid black;border-bottom:none">资产部意见:{{propertyst}}</td>
                      </tr>
                      <tr style="border-top:0px solid white;">  
                        <td colspan="8" align="right" style="border:1px solid black;border-top:none"><span>签字:{{propertystName}}</span><span>日期:{{propertystDate}}</span></td>
                      </tr>
                     
                    </tbody>
                  </table>
        				</div>
        			</el-dialog>
    

    css代码:

     #printTest1 table{
          border-collapse:collapse;
        }
        #printTest1 table thead th{
          font-size: 20px;
          padding:10px;
        }
        #printTest1 table tbody tr{
          height:30px;
          font-size:14px;
        }
        #printTest1 table tbody td{
          width:25%;
        }
        #printTest1 table tbody td span{
          margin-right:20px;
        } 
    

    第四种和第一种是一样的只不过是放在了vue-cli脚手架里在这里插入图片描述
    代码:

      <el-dialog title="材料出库单" v-dialogDrag :close-on-click-modal="false" :visible.sync="printDialogVisible" width="60%">
                              <el-button type="success" v-print="'#printTest'"><svg-icon icon-class="print"/>打印</el-button>
                              <div id="printTest">
                                  <div style="font-size: 22px;padding-bottom:10px;font-weight:800;font-family:宋体; text-align:center">出库单</div>
                                  <span style="font-size:16px;font-family:宋体;float:left;">客户名称:</span>
                                  <span style="font-size:16px;font-family:宋体;float:right;">出库日期:20190803</span>
                                  	<table>
                                     <thead>
                                        <th>材料编号</th>
                                        <th>材料名称</th>
                                        <th>单位</th>
                                        <th>订单号</th>
                                        <th>数量</th>
                                        <th>箱数</th>
                                        <th>备注</th>                                 
                                     </thead>
                                       <tbody>
                                            <tr v-for="item in tableData" :key="item.id">
                                                <td>{{item.date}}</td>
                                                <td>{{item.name}}</td>
                                                <td>{{item.address}}</td>
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                            </tr>
                                            </tbody>
                                      </table>
                                </div>
                              </el-dialog>
    

    css代码:

       #printTest table{
            font-family:"宋体";
            border-collapse:collapse;
            width:99.5%;
        }
        #printTest table thead th{
            height: 40px;
            font-size: 13px;
            /* width: 10px; */
            text-align: center;
            border: 1px solid black;
        }
        #printTest table tbody tr{
            font-size:13px;
            border: 1px solid black;
            height: 40px;
            text-align: center;
        }
        #printTest table tbody td{
            font-size: 13px;
            text-align: center;
            border: 1px solid black;
        }
    
    展开全文
  • 在实现打印功能的时候,打印预览表格都显示出来,打印的时候表格有缺失,是怎麽回事
  • 基于C#关于Winform打印表格 / 输出到Excel打印的操作前言主页面 Form1.cs[设计] 介绍表格基础设计 From.Designer.cs表格打印设置 From.cs合并单元格,并将所有单元格设置为居中 From.cs插入数据 From.cs将表格导出到...

    前言

    最近有个需求,是需要用WinForm打印表格,打印机是针式打印机,纸张则为发票联式的纸张(一联大小大概20cm*10cm),本文列举了两个打印方法,一个是用WinForm自带的打印类,另一个则是将表格输出到Excel后再打印。

    主页面 Form1.cs[设计] 介绍

    首先先来看下页面的主要设计:

    页面截图1

    1. RowMergeView ,这里涉及到一个问题,就是表格要求过长字符换行并且有的单元格需要合并,但是如果使用DataGridView合并单元格,则合并了之后系统只是将两个单元格之间的边框变成白色,实际上还是两个单元格,这样换行的字符就无法正常显示,所以用了别人的RowMergeView插件;
    2. Panel嵌套Label和RowMergeView,直接打印Panel。

    表格基础设计 From.Designer.cs

    namespace WindowsApplication15
    {
        partial class Form1
        {
            /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows 窗体设计器生成的代码
    
            /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
                System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
                System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
                System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle();
                System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle4 = new System.Windows.Forms.DataGridViewCellStyle();
                System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle5 = new System.Windows.Forms.DataGridViewCellStyle();
                System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle6 = new System.Windows.Forms.DataGridViewCellStyle();
                System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle7 = new System.Windows.Forms.DataGridViewCellStyle();
                System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle8 = new System.Windows.Forms.DataGridViewCellStyle();
                this.panel1 = new System.Windows.Forms.Panel();
                this.button3 = new System.Windows.Forms.Button();
                this.button2 = new System.Windows.Forms.Button();
                this.button1 = new System.Windows.Forms.Button();
                this.printDocument1 = new System.Drawing.Printing.PrintDocument();
                this.label1 = new System.Windows.Forms.Label();
                this.label2 = new System.Windows.Forms.Label();
                this.label3 = new System.Windows.Forms.Label();
                this.panel2 = new System.Windows.Forms.Panel();
                this.pageSetupDialog1 = new System.Windows.Forms.PageSetupDialog();
                this.printDialog1 = new System.Windows.Forms.PrintDialog();
                this.printPreviewDialog1 = new System.Windows.Forms.PrintPreviewDialog();
                this.button4 = new System.Windows.Forms.Button();
                this.rowMergeView1 = new RowMergeView();
                this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
                this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
                this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
                this.Column4 = new System.Windows.Forms.DataGridViewTextBoxColumn();
                this.Column5 = new System.Windows.Forms.DataGridViewTextBoxColumn();
                this.Column6 = new System.Windows.Forms.DataGridViewTextBoxColumn();
                this.Column7 = new System.Windows.Forms.DataGridViewTextBoxColumn();
                this.panel1.SuspendLayout();
                this.panel2.SuspendLayout();
                ((System.ComponentModel.ISupportInitialize)(this.rowMergeView1)).BeginInit();
                this.SuspendLayout();
                // 
                // panel1
                // 
                this.panel1.Controls.Add(this.button4);
                this.panel1.Controls.Add(this.button3);
                this.panel1.Controls.Add(this.button2);
                this.panel1.Controls.Add(this.button1);
                this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
                this.panel1.Location = new System.Drawing.Point(0, 386);
                this.panel1.Name = "panel1";
                this.panel1.Size = new System.Drawing.Size(751, 34);
                this.panel1.TabIndex = 0;
                // 
                // button3
                // 
                this.button3.Location = new System.Drawing.Point(243, 4);
                this.button3.Name = "button3";
                this.button3.Size = new System.Drawing.Size(75, 23);
                this.button3.TabIndex = 2;
                this.button3.Text = "打印预览";
                this.button3.UseVisualStyleBackColor = true;
                this.button3.Click += new System.EventHandler(this.button3_Click);
                // 
                // button2
                // 
                this.button2.Location = new System.Drawing.Point(126, 4);
                this.button2.Name = "button2";
                this.button2.Size = new System.Drawing.Size(75, 23);
                this.button2.TabIndex = 1;
                this.button2.Text = "打印设置";
                this.button2.UseVisualStyleBackColor = true;
                this.button2.Click += new System.EventHandler(this.button2_Click);
                // 
                // button1
                // 
                this.button1.Anchor = System.Windows.Forms.AnchorStyles.None;
                this.button1.Location = new System.Drawing.Point(12, 5);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(75, 23);
                this.button1.TabIndex = 0;
                this.button1.Text = "打印";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // printDocument1
                // 
                this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
                // 
                // label1
                // 
                this.label1.AutoSize = true;
                this.label1.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                this.label1.Location = new System.Drawing.Point(201, 14);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(309, 19);
                this.label1.TabIndex = 2;
                this.label1.Text = "膳食服务中心新院网络订餐配送单";
                this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
                // 
                // label2
                // 
                this.label2.AutoSize = true;
                this.label2.Location = new System.Drawing.Point(12, 48);
                this.label2.Name = "label2";
                this.label2.Size = new System.Drawing.Size(41, 12);
                this.label2.TabIndex = 3;
                this.label2.Text = "日期:";
                // 
                // label3
                // 
                this.label3.AutoSize = true;
                this.label3.Location = new System.Drawing.Point(12, 64);
                this.label3.Name = "label3";
                this.label3.Size = new System.Drawing.Size(65, 12);
                this.label3.TabIndex = 4;
                this.label3.Text = "套餐类型:";
                // 
                // panel2
                // 
                this.panel2.Controls.Add(this.label3);
                this.panel2.Controls.Add(this.label2);
                this.panel2.Controls.Add(this.label1);
                this.panel2.Controls.Add(this.rowMergeView1);
                this.panel2.Location = new System.Drawing.Point(0, 12);
                this.panel2.Name = "panel2";
                this.panel2.Size = new System.Drawing.Size(740, 374);
                this.panel2.TabIndex = 5;
                // 
                // printDialog1
                // 
                this.printDialog1.UseEXDialog = true;
                // 
                // printPreviewDialog1
                // 
                this.printPreviewDialog1.AutoScrollMargin = new System.Drawing.Size(0, 0);
                this.printPreviewDialog1.AutoScrollMinSize = new System.Drawing.Size(0, 0);
                this.printPreviewDialog1.ClientSize = new System.Drawing.Size(400, 300);
                this.printPreviewDialog1.Enabled = true;
                this.printPreviewDialog1.Icon = ((System.Drawing.Icon)(resources.GetObject("printPreviewDialog1.Icon")));
                this.printPreviewDialog1.Name = "printPreviewDialog1";
                this.printPreviewDialog1.Visible = false;
                // 
                // button4
                // 
                this.button4.Location = new System.Drawing.Point(355, 3);
                this.button4.Name = "button4";
                this.button4.Size = new System.Drawing.Size(75, 23);
                this.button4.TabIndex = 3;
                this.button4.Text = "导出";
                this.button4.UseVisualStyleBackColor = true;
                this.button4.Click += new System.EventHandler(this.button4_Click);
                // 
                // rowMergeView1
                // 
                this.rowMergeView1.AllowUserToAddRows = false;
                this.rowMergeView1.AllowUserToDeleteRows = false;
                this.rowMergeView1.BackgroundColor = System.Drawing.Color.White;
                dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
                dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
                dataGridViewCellStyle1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
                dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
                dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
                dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
                this.rowMergeView1.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
                this.rowMergeView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                this.rowMergeView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
                this.Column1,
                this.Column2,
                this.Column3,
                this.Column4,
                this.Column5,
                this.Column6,
                this.Column7});
                this.rowMergeView1.Location = new System.Drawing.Point(14, 95);
                this.rowMergeView1.MergeColumnHeaderBackColor = System.Drawing.SystemColors.Control;
                this.rowMergeView1.MergeColumnNames = ((System.Collections.Generic.List<string>)(resources.GetObject("rowMergeView1.MergeColumnNames")));
                this.rowMergeView1.Name = "rowMergeView1";
                this.rowMergeView1.ReadOnly = true;
                this.rowMergeView1.RowTemplate.Height = 23;
                this.rowMergeView1.Size = new System.Drawing.Size(713, 242);
                this.rowMergeView1.TabIndex = 1;
                this.rowMergeView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.rowMergeView1_CellContentClick);
                // 
                // Column1
                // 
                this.Column1.DataPropertyName = "1";
                dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
                this.Column1.DefaultCellStyle = dataGridViewCellStyle2;
                this.Column1.HeaderText = "地址";
                this.Column1.Name = "Column1";
                this.Column1.ReadOnly = true;
                this.Column1.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
                this.Column1.Width = 70;
                // 
                // Column2
                // 
                this.Column2.DataPropertyName = "2";
                dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
                this.Column2.DefaultCellStyle = dataGridViewCellStyle3;
                this.Column2.HeaderText = "套餐名称";
                this.Column2.Name = "Column2";
                this.Column2.ReadOnly = true;
                this.Column2.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
                this.Column2.Width = 120;
                // 
                // Column3
                // 
                this.Column3.DataPropertyName = "3";
                dataGridViewCellStyle4.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
                this.Column3.DefaultCellStyle = dataGridViewCellStyle4;
                this.Column3.HeaderText = "数量";
                this.Column3.Name = "Column3";
                this.Column3.ReadOnly = true;
                this.Column3.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
                this.Column3.Width = 60;
                // 
                // Column4
                // 
                this.Column4.DataPropertyName = "4";
                dataGridViewCellStyle5.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
                this.Column4.DefaultCellStyle = dataGridViewCellStyle5;
                this.Column4.HeaderText = "订餐人员";
                this.Column4.Name = "Column4";
                this.Column4.ReadOnly = true;
                this.Column4.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
                this.Column4.Width = 90;
                // 
                // Column5
                // 
                this.Column5.DataPropertyName = "5";
                dataGridViewCellStyle6.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
                this.Column5.DefaultCellStyle = dataGridViewCellStyle6;
                this.Column5.HeaderText = "联系电话";
                this.Column5.Name = "Column5";
                this.Column5.ReadOnly = true;
                this.Column5.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
                this.Column5.Width = 90;
                // 
                // Column6
                // 
                this.Column6.DataPropertyName = "6";
                dataGridViewCellStyle7.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
                this.Column6.DefaultCellStyle = dataGridViewCellStyle7;
                this.Column6.HeaderText = "份数汇总";
                this.Column6.Name = "Column6";
                this.Column6.ReadOnly = true;
                this.Column6.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
                this.Column6.Width = 180;
                // 
                // Column7
                // 
                this.Column7.DataPropertyName = "7";
                dataGridViewCellStyle8.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
                this.Column7.DefaultCellStyle = dataGridViewCellStyle8;
                this.Column7.HeaderText = "交接签名";
                this.Column7.Name = "Column7";
                this.Column7.ReadOnly = true;
                this.Column7.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(751, 420);
                this.Controls.Add(this.panel2);
                this.Controls.Add(this.panel1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.panel1.ResumeLayout(false);
                this.panel2.ResumeLayout(false);
                this.panel2.PerformLayout();
                ((System.ComponentModel.ISupportInitialize)(this.rowMergeView1)).EndInit();
                this.ResumeLayout(false);
    
            }
    
            #endregion
    
            private System.Windows.Forms.Panel panel1;
            private System.Windows.Forms.Button button1;
            private System.Drawing.Printing.PrintDocument printDocument1;
            private System.Windows.Forms.Label label1;
            private RowMergeView rowMergeView1;
            private System.Windows.Forms.DataGridViewTextBoxColumn Column1;
            private System.Windows.Forms.DataGridViewTextBoxColumn Column2;
            private System.Windows.Forms.DataGridViewTextBoxColumn Column3;
            private System.Windows.Forms.DataGridViewTextBoxColumn Column4;
            private System.Windows.Forms.DataGridViewTextBoxColumn Column5;
            private System.Windows.Forms.DataGridViewTextBoxColumn Column6;
            private System.Windows.Forms.DataGridViewTextBoxColumn Column7;
            private System.Windows.Forms.Label label2;
            private System.Windows.Forms.Label label3;
            private System.Windows.Forms.Panel panel2;
            private System.Windows.Forms.PageSetupDialog pageSetupDialog1;
            private System.Windows.Forms.PrintDialog printDialog1;
            private System.Windows.Forms.PrintPreviewDialog printPreviewDialog1;
            private System.Windows.Forms.Button button3;
            private System.Windows.Forms.Button button2;
            private System.Windows.Forms.Button button4;
        }
    }
    

    表格打印设置 From.cs

    这里用Bitmap直接将页面打印出来,如果用网上常见的DataGridView打印类,上面的Label则不会被打印出来

    		//打印
            private void button1_Click(object sender, EventArgs e) {
                if (this.printDialog1.ShowDialog() == DialogResult.OK) {
                    this.printDocument1.Print();
                }
            }
    
            //打印设置
            private void button2_Click(object sender, EventArgs e) {
                this.pageSetupDialog1.ShowDialog();
            }
    
            //打印预览
            private void button3_Click(object sender, EventArgs e) {
                this.printPreviewDialog1.ShowDialog();
            }
    
            //打印容器设置
            private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) {
                Bitmap _NewBitmap = new Bitmap(panel2.Width - 2, panel2.Height - 20);
                panel2.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
                e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height);
            }
    

    合并单元格,并将所有单元格设置为居中 From.cs

    			this.rowMergeView1.MergeColumnNames.Add("Column1");     //合并Column1该列文本相同的单元格
                this.rowMergeView1.MergeColumnNames.Add("Column6");
                this.rowMergeView1.MergeColumnNames.Add("Column7");
    
                //设置自动换行
                this.rowMergeView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
                //设置自动调整高度
                this.rowMergeView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
                //禁止最左边的空列头显示
                this.rowMergeView1.RowHeadersVisible = false;
    

    插入数据 From.cs

    这里先不用数据库导入,先采用收到录入数据方式,来验证格式是否可用

    			DataTable dt = new DataTable();
                dt.Columns.Add("1");
                dt.Columns.Add("2");
                dt.Columns.Add("3");
                dt.Columns.Add("4");
                dt.Columns.Add("5");
                dt.Columns.Add("6");
                dt.Columns.Add("7");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "香炸鸡腿套餐A", "1", "杨文静", "679357", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "鱼香肉丝套餐B", "1", "刘林荣", "670327", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "鱼香肉丝套餐C", "1", "刘林荣", "670327", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "鱼香肉丝套餐D", "1", "刘林荣", "670327", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "鱼香肉丝套餐E", "1", "刘林荣", "670327", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "鱼香肉丝套餐F", "1", "刘林荣", "670327", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "鱼香肉丝套餐G", "1", "刘林荣", "670327", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "鱼香肉丝套餐H", "1", "刘林荣", "670327", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "鱼香肉丝套餐I", "1", "刘林荣", "670327", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "鱼香肉丝套餐J", "1", "刘林荣", "670327", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "鱼香肉丝套餐K", "1", "刘林荣", "670327", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "鱼香肉丝套餐L", "1", "刘林荣", "670327", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "鱼香肉丝套餐M", "1", "刘林荣", "670327", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "鱼香肉丝套餐N", "1", "刘林荣", "670327", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "鱼香肉丝套餐O", "1", "刘林荣", "670327", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
    
                this.rowMergeView1.DataSource = dt;
    

    将表格导出到Excel From.cs

    导出到Excel时可以同时设置Excel的格式,都在代码里,有注释

    		private void button4_Click(object sender, EventArgs e) {
                string a = "D:" + "\\膳食服务中心新院网络订餐配送单.xls";
                ExportExcels(a, rowMergeView1);
            }
    		
    		/// <summary>
            ///
            /// </summary>
            /// <param name="fileName">文件路径</param>
            /// <param name="myDGV">控件DataGridView</param>
            private void ExportExcels(string fileName, RowMergeView 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
    
                int rowCount = myDGV.Rows.Count;           //行数
                int columnCount = myDGV.Columns.Count;     //列数
                //写入标题
                for (int i = 0; i < myDGV.ColumnCount; i++) {
                    worksheet.Cells[4, i + 1] = myDGV.Columns[i].HeaderText;
                }
    
                //写入数值
                for (int r = 0; r < myDGV.RowCount; r++) {
                    for (int i = 0; i < myDGV.ColumnCount; i++) {
                        worksheet.Cells[r + 5, i + 1] = myDGV.Rows[r].Cells[i].Value;
                    }
                    Application.DoEvents();
                }
    
                //格式设置
                
                worksheet.Columns.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;     //所有单元格水平居中
                worksheet.Columns.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;       //垂直居中
                worksheet.get_Range(worksheet.Cells[4, 1], worksheet.Cells[rowCount + 4, columnCount]).Borders.LineStyle = 1;       //设置选定区域单元格边框,从第4行第1列开始绘制边框
                //worksheet.Borders.LineStyle=1;     //设置所有单元格边框的粗细
                worksheet.Cells[1, 1] = "膳食服务中心新院网络订餐配送单";       //Excel单元格赋值
                worksheet.Cells[2, 1] = "日期:";       //Excel单元格赋值
                worksheet.get_Range("A2").HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;       //A2单元格水平靠左
                worksheet.Cells[3, 1] = "套餐类型:";       //Excel单元格赋值
                worksheet.get_Range("A3").HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;       //A3单元格水平靠左
    
                worksheet.get_Range("A1", "G1").Merge(worksheet.get_Range("A1", "G1").MergeCells);      //合并A1-G1的单元格
                worksheet.get_Range("A2", "G2").Merge(worksheet.get_Range("A2", "G2").MergeCells);      //合并A2-G2的单元格
                worksheet.get_Range("A3", "G3").Merge(worksheet.get_Range("A3", "G3").MergeCells);      //合并A3-G3的单元格
    
                worksheet.get_Range("A1").ColumnWidth = 8.38;      //设置A列宽
                worksheet.get_Range("B1").ColumnWidth = 15.88;      //设置B列宽
                worksheet.get_Range("C1").ColumnWidth = 8.38;      //设置C列宽
                worksheet.get_Range("D1").ColumnWidth = 8.38;      //设置D列宽
                worksheet.get_Range("E1").ColumnWidth = 8.38;      //设置E列宽
                worksheet.get_Range("F1").ColumnWidth = 20.88;      //设置F列宽
                worksheet.get_Range("G1").ColumnWidth = 13;      //设置G列宽
    
                //合并选定区域单元格,从第5行第1列开始合并
                worksheet.get_Range(worksheet.Cells[5, 1], worksheet.Cells[rowCount + 4, 1]).Merge(worksheet.get_Range(worksheet.Cells[5, 1], worksheet.Cells[rowCount + 4, 1]).MergeCells);
                //合并选定区域单元格,从第5行第6列开始合并
                worksheet.get_Range(worksheet.Cells[5, 6], worksheet.Cells[rowCount + 4, 6]).Merge(worksheet.get_Range(worksheet.Cells[5, 6], worksheet.Cells[rowCount + 4, 6]).MergeCells);
                //合并选定区域单元格,从第5行第7列开始合并
                worksheet.get_Range(worksheet.Cells[5, 7], worksheet.Cells[rowCount + 4, 7]).Merge(worksheet.get_Range(worksheet.Cells[5, 7], worksheet.Cells[rowCount + 4, 7]).MergeCells);
    
                worksheet.Columns.EntireColumn.AutoFit();       //列宽自适应
                worksheet.Rows.EntireRow.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);
            }
    

    完整的 From.cs

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Printing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsApplication15 {
        public partial class Form1 : Form {
            public Form1()
            {
                InitializeComponent();
                DataTable dt = new DataTable();
                dt.Columns.Add("1");
                dt.Columns.Add("2");
                dt.Columns.Add("3");
                dt.Columns.Add("4");
                dt.Columns.Add("5");
                dt.Columns.Add("6");
                dt.Columns.Add("7");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "香炸鸡腿套餐A", "1", "杨文静", "679357", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "鱼香肉丝套餐B", "1", "刘林荣", "670327", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "鱼香肉丝套餐C", "1", "刘林荣", "670327", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "鱼香肉丝套餐D", "1", "刘林荣", "670327", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "鱼香肉丝套餐E", "1", "刘林荣", "670327", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "鱼香肉丝套餐F", "1", "刘林荣", "670327", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "鱼香肉丝套餐G", "1", "刘林荣", "670327", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "鱼香肉丝套餐H", "1", "刘林荣", "670327", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "鱼香肉丝套餐I", "1", "刘林荣", "670327", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "鱼香肉丝套餐J", "1", "刘林荣", "670327", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "鱼香肉丝套餐K", "1", "刘林荣", "670327", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "鱼香肉丝套餐L", "1", "刘林荣", "670327", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "鱼香肉丝套餐M", "1", "刘林荣", "670327", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "鱼香肉丝套餐N", "1", "刘林荣", "670327", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
                dt.Rows.Add("122病区" + "\r\n" + "护士台", "鱼香肉丝套餐O", "1", "刘林荣", "670327", "香炸鸡腿套餐C共1份;" + "\r\n" + "鱼香肉丝套餐A共1份;" + "\r\n" + "套餐总共份数2", "");
    
                this.rowMergeView1.DataSource = dt;
    
                //this.rowMergeView1.ColumnHeadersHeight = 40;
                //this.rowMergeView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
                this.rowMergeView1.MergeColumnNames.Add("Column1");     //合并Column1该列文本相同的单元格
                this.rowMergeView1.MergeColumnNames.Add("Column6");
                this.rowMergeView1.MergeColumnNames.Add("Column7");
    
                //设置自动换行
                this.rowMergeView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
                //设置自动调整高度
                this.rowMergeView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
                //禁止最左边的空列头显示
                this.rowMergeView1.RowHeadersVisible = false;
            }
            
            private void rowMergeView1_CellContentClick(object sender, DataGridViewCellEventArgs e) {
    
            }
    
            private void label1_Click(object sender, EventArgs e) {
    
            }
    
            private void panel1_Paint(object sender, PaintEventArgs e) {
    
            }
    
            //打印
            private void button1_Click(object sender, EventArgs e) {
                if (this.printDialog1.ShowDialog() == DialogResult.OK) {
                    this.printDocument1.Print();
                }
            }
    
            //打印设置
            private void button2_Click(object sender, EventArgs e) {
                this.pageSetupDialog1.ShowDialog();
            }
    
            //打印预览
            private void button3_Click(object sender, EventArgs e) {
                this.printPreviewDialog1.ShowDialog();
            }
    
            //打印容器设置
            private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) {
                Bitmap _NewBitmap = new Bitmap(panel2.Width - 2, panel2.Height - 20);
                panel2.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
                e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height);
            }
    
            private void button4_Click(object sender, EventArgs e) {
                string a = "D:" + "\\膳食服务中心新院网络订餐配送单.xls";
                ExportExcels(a, rowMergeView1);
            }
    
            /// <summary>
            ///
            /// </summary>
            /// <param name="fileName">文件路径</param>
            /// <param name="myDGV">控件DataGridView</param>
            private void ExportExcels(string fileName, RowMergeView 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
    
                int rowCount = myDGV.Rows.Count;           //行数
                int columnCount = myDGV.Columns.Count;     //列数
                //写入标题
                for (int i = 0; i < myDGV.ColumnCount; i++) {
                    worksheet.Cells[4, i + 1] = myDGV.Columns[i].HeaderText;
                }
    
                //写入数值
                for (int r = 0; r < myDGV.RowCount; r++) {
                    for (int i = 0; i < myDGV.ColumnCount; i++) {
                        worksheet.Cells[r + 5, i + 1] = myDGV.Rows[r].Cells[i].Value;
                    }
                    Application.DoEvents();
                }
    
                //格式设置
                
                worksheet.Columns.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;     //所有单元格水平居中
                worksheet.Columns.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;       //垂直居中
                worksheet.get_Range(worksheet.Cells[4, 1], worksheet.Cells[rowCount + 4, columnCount]).Borders.LineStyle = 1;       //设置选定区域单元格边框,从第4行第1列开始绘制边框
                //worksheet.Borders.LineStyle=1;     //设置所有单元格边框的粗细
                worksheet.Cells[1, 1] = "膳食服务中心新院网络订餐配送单";       //Excel单元格赋值
                worksheet.Cells[2, 1] = "日期:";       //Excel单元格赋值
                worksheet.get_Range("A2").HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;       //A2单元格水平靠左
                worksheet.Cells[3, 1] = "套餐类型:";       //Excel单元格赋值
                worksheet.get_Range("A3").HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;       //A3单元格水平靠左
    
                worksheet.get_Range("A1", "G1").Merge(worksheet.get_Range("A1", "G1").MergeCells);      //合并A1-G1的单元格
                worksheet.get_Range("A2", "G2").Merge(worksheet.get_Range("A2", "G2").MergeCells);      //合并A2-G2的单元格
                worksheet.get_Range("A3", "G3").Merge(worksheet.get_Range("A3", "G3").MergeCells);      //合并A3-G3的单元格
    
                worksheet.get_Range("A1").ColumnWidth = 8.38;      //设置A列宽
                worksheet.get_Range("B1").ColumnWidth = 15.88;      //设置B列宽
                worksheet.get_Range("C1").ColumnWidth = 8.38;      //设置C列宽
                worksheet.get_Range("D1").ColumnWidth = 8.38;      //设置D列宽
                worksheet.get_Range("E1").ColumnWidth = 8.38;      //设置E列宽
                worksheet.get_Range("F1").ColumnWidth = 20.88;      //设置F列宽
                worksheet.get_Range("G1").ColumnWidth = 13;      //设置G列宽
    
                //合并选定区域单元格,从第5行第1列开始合并
                worksheet.get_Range(worksheet.Cells[5, 1], worksheet.Cells[rowCount + 4, 1]).Merge(worksheet.get_Range(worksheet.Cells[5, 1], worksheet.Cells[rowCount + 4, 1]).MergeCells);
                //合并选定区域单元格,从第5行第6列开始合并
                worksheet.get_Range(worksheet.Cells[5, 6], worksheet.Cells[rowCount + 4, 6]).Merge(worksheet.get_Range(worksheet.Cells[5, 6], worksheet.Cells[rowCount + 4, 6]).MergeCells);
                //合并选定区域单元格,从第5行第7列开始合并
                worksheet.get_Range(worksheet.Cells[5, 7], worksheet.Cells[rowCount + 4, 7]).Merge(worksheet.get_Range(worksheet.Cells[5, 7], worksheet.Cells[rowCount + 4, 7]).MergeCells);
    
                worksheet.Columns.EntireColumn.AutoFit();       //列宽自适应
                worksheet.Rows.EntireRow.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);
            }
        }
    }
    

    RowMergeView.cs

    也放一下大佬写的RowMergeView吧

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Design;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;
    using System.Reflection;
    using System.Runtime.InteropServices;
    
        /// <summary>
        /// DataGridView行合并.请对属性MergeColumnNames 赋值既可
        /// </summary>
        public partial class RowMergeView : DataGridView
        {
            #region 构造函数
            public RowMergeView()
            {
                InitializeComponent();
            }
            #endregion
            #region 重写的事件
            protected override void OnPaint(PaintEventArgs pe)
            {
                // TODO: 在此处添加自定义绘制代码
    
                // 调用基类 OnPaint
                base.OnPaint(pe);
            }
            protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
            {
                try
                {
                    if (e.RowIndex > -1 && e.ColumnIndex > -1)
                    {
                        DrawCell(e);
                    }
                    else
                    {
                        //二维表头
                        if (e.RowIndex == -1)
                        {
                            if (SpanRows.ContainsKey(e.ColumnIndex)) //被合并的列
                            {
                                //画边框
                                Graphics g = e.Graphics;
                                e.Paint(e.CellBounds, DataGridViewPaintParts.Background | DataGridViewPaintParts.Border);
    
                                int left = e.CellBounds.Left, top = e.CellBounds.Top + 2,
                                right = e.CellBounds.Right, bottom = e.CellBounds.Bottom;
    
                                switch (SpanRows[e.ColumnIndex].Position)
                                {
                                    case 1:
                                        left += 2;
                                        break;
                                    case 2:
                                        break;
                                    case 3:
                                        right -= 2;
                                        break;
                                }
    
                                //画上半部分底色
                                g.FillRectangle(new SolidBrush(this._mergecolumnheaderbackcolor), left, top,
                                right - left, (bottom - top) / 2);
    
                                //画中线
                                g.DrawLine(new Pen(this.GridColor), left, (top + bottom) / 2,
                                right, (top + bottom) / 2);
    
                                //写小标题
                                StringFormat sf = new StringFormat();
                                sf.Alignment = StringAlignment.Center;
                                sf.LineAlignment = StringAlignment.Center;
    
                                g.DrawString(e.Value + "", e.CellStyle.Font, Brushes.Black,
                                new Rectangle(left, (top + bottom) / 2, right - left, (bottom - top) / 2), sf);
                                left = this.GetColumnDisplayRectangle(SpanRows[e.ColumnIndex].Left, true).Left - 2;
    
                                if (left < 0) left = this.GetCellDisplayRectangle(-1, -1, true).Width;
                                right = this.GetColumnDisplayRectangle(SpanRows[e.ColumnIndex].Right, true).Right - 2;
                                if (right < 0) right = this.Width;
    
                                g.DrawString(SpanRows[e.ColumnIndex].Text, e.CellStyle.Font, Brushes.Black,
                                new Rectangle(left, top, right - left, (bottom - top) / 2), sf);
                                e.Handled = true;
                            }
                        }
                    }
                    base.OnCellPainting(e);
                }
                catch
                { }
            }
            protected override void OnCellClick(DataGridViewCellEventArgs e)
            {
                base.OnCellClick(e);
            }
            #endregion
            #region 自定义方法
            /// <summary>
            /// 画单元格
            /// </summary>
            /// <param name="e"></param>
            private void DrawCell(DataGridViewCellPaintingEventArgs e)
            {
                if (e.CellStyle.Alignment == DataGridViewContentAlignment.NotSet)
                {
                    e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                }
                Brush gridBrush = new SolidBrush(this.GridColor);
                SolidBrush backBrush = new SolidBrush(e.CellStyle.BackColor);
                SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor);
                int cellwidth;
                //上面相同的行数
                int UpRows = 0;
                //下面相同的行数
                int DownRows = 0;
                //总行数
                int count = 0;
                if (this.MergeColumnNames.Contains(this.Columns[e.ColumnIndex].Name) && e.RowIndex != -1)
                {
                    cellwidth = e.CellBounds.Width;
                    Pen gridLinePen = new Pen(gridBrush);
                    string curValue = e.Value == null ? "" : e.Value.ToString().Trim();
                    string curSelected = this.CurrentRow.Cells[e.ColumnIndex].Value == null ? "" : this.CurrentRow.Cells[e.ColumnIndex].Value.ToString().Trim();
                    if (!string.IsNullOrEmpty(curValue))
                    {
                        #region 获取下面的行数
                        for (int i = e.RowIndex; i < this.Rows.Count; i++)
                        {
                            if (this.Rows[i].Cells[e.ColumnIndex].Value.ToString().Equals(curValue))
                            {
                                //this.Rows[i].Cells[e.ColumnIndex].Selected = this.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected;
                                
                                DownRows++;
                                if (e.RowIndex != i)
                                {
                                    cellwidth = cellwidth < this.Rows[i].Cells[e.ColumnIndex].Size.Width ? cellwidth : this.Rows[i].Cells[e.ColumnIndex].Size.Width;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                        #endregion
                        #region 获取上面的行数
                        for (int i = e.RowIndex; i >= 0; i--)
                        {
                            if (this.Rows[i].Cells[e.ColumnIndex].Value.ToString().Equals(curValue))
                            {
                                //this.Rows[i].Cells[e.ColumnIndex].Selected = this.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected;
                                UpRows++;
                                if (e.RowIndex != i)
                                {
                                    cellwidth = cellwidth < this.Rows[i].Cells[e.ColumnIndex].Size.Width ? cellwidth : this.Rows[i].Cells[e.ColumnIndex].Size.Width;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                        #endregion
                        count = DownRows + UpRows - 1;
                        if (count < 2)
                        {
                            return;
                        }
                    }
                    if (this.Rows[e.RowIndex].Selected)
                    {
                        backBrush.Color = e.CellStyle.SelectionBackColor;
                        fontBrush.Color = e.CellStyle.SelectionForeColor;
                    }
                    //以背景色填充
                    e.Graphics.FillRectangle(backBrush, e.CellBounds);
                    //画字符串
                    PaintingFont(e, cellwidth, UpRows, DownRows, count);
                    if (DownRows == 1)
                    {
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                        count = 0;
                    }
                    // 画右边线
                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
    
                    e.Handled = true;
                }
            }
            /// <summary>
            /// 画字符串
            /// </summary>
            /// <param name="e"></param>
            /// <param name="cellwidth"></param>
            /// <param name="UpRows"></param>
            /// <param name="DownRows"></param>
            /// <param name="count"></param>
            private void PaintingFont(System.Windows.Forms.DataGridViewCellPaintingEventArgs e, int cellwidth, int UpRows, int DownRows, int count)
            {
                SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor);
                int fontheight = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Height;
                int fontwidth = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Width;
                int cellheight = e.CellBounds.Height;
    
                if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomCenter)
                {
                    e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y + cellheight * DownRows - fontheight);
                }
                else if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomLeft)
                {
                    e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, e.CellBounds.X, e.CellBounds.Y + cellheight * DownRows - fontheight);
                }
                else if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomRight)
                {
                    e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y + cellheight * DownRows - fontheight);
                }
                else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleCenter)
                {
                    e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
                }
                else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleLeft)
                {
                    e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, e.CellBounds.X, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
                }
                else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleRight)
                {
                    e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
                }
                else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopCenter)
                {
                    e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1));
                }
                else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopLeft)
                {
                    e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, e.CellBounds.X, e.CellBounds.Y - cellheight * (UpRows - 1));
                }
                else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopRight)
                {
                    e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y - cellheight * (UpRows - 1));
                }
                else
                {
                    e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
                }
            }
            #endregion
            #region 属性
            /// <summary>
            /// 设置或获取合并列的集合
            /// </summary>
            [MergableProperty(false)]
            [Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
            [DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Visible)]
            [Localizable(true)]
            [Description("设置或获取合并列的集合"), Browsable(true), Category("单元格合并")]
            public List<string> MergeColumnNames
            {
                get
                {
                    return _mergecolumnname;
                }
                set
                {
                    _mergecolumnname = value;
                }
            }
            private List<string> _mergecolumnname = new List<string>();
            #endregion
            #region 二维表头
            private struct SpanInfo //表头信息
            {
                public SpanInfo(string Text, int Position, int Left, int Right)
                {
                    this.Text = Text;
                    this.Position = Position;
                    this.Left = Left;
                    this.Right = Right;
                }
    
                public string Text; //列主标题
                public int Position; //位置,1:左,2中,3右
                public int Left; //对应左行
                public int Right; //对应右行
            }
            private Dictionary<int, SpanInfo> SpanRows = new Dictionary<int, SpanInfo>();//需要2维表头的列
            /// <summary>
            /// 合并列
            /// </summary>
            /// <param name="ColIndex">列的索引</param>
            /// <param name="ColCount">需要合并的列数</param>
            /// <param name="Text">合并列后的文本</param>
            public void AddSpanHeader(int ColIndex, int ColCount, string Text)
            {
                if (ColCount < 2)
                {
                    throw new Exception("行宽应大于等于2,合并1列无意义。");
                }
                //将这些列加入列表
                int Right = ColIndex + ColCount - 1; //同一大标题下的最后一列的索引
                SpanRows[ColIndex] = new SpanInfo(Text, 1, ColIndex, Right); //添加标题下的最左列
                SpanRows[Right] = new SpanInfo(Text, 3, ColIndex, Right); //添加该标题下的最右列
                for (int i = ColIndex + 1; i < Right; i++) //中间的列
                {
                    SpanRows[i] = new SpanInfo(Text, 2, ColIndex, Right);
                }
            }
            /// <summary>
            /// 清除合并的列
            /// </summary>
            public void ClearSpanInfo()
            {
                SpanRows.Clear();
                //ReDrawHead();
            }
            private void DataGridViewEx_Scroll(object sender, ScrollEventArgs e)
            {
                if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)// && e.Type == ScrollEventType.EndScroll)
                {
                    timer1.Enabled = false; timer1.Enabled = true;
                }
            }
            //刷新显示表头
            public void ReDrawHead()
            {
                foreach (int si in SpanRows.Keys)
                {
                    this.Invalidate(this.GetCellDisplayRectangle(si, -1, true));
                }
            }
            private void timer1_Tick(object sender, EventArgs e)
            {
                timer1.Enabled = false;
                ReDrawHead();
            }
            /// <summary>
            /// 二维表头的背景颜色
            /// </summary>
            [Description("二维表头的背景颜色"), Browsable(true), Category("二维表头")]
            public Color MergeColumnHeaderBackColor
            {
                get { return this._mergecolumnheaderbackcolor; }
                set { this._mergecolumnheaderbackcolor = value; }
            }
            private Color _mergecolumnheaderbackcolor = System.Drawing.SystemColors.Control;
        #endregion
    
        private void timer1_Tick_1(object sender, EventArgs e) {
    
        }
    }
    

    源代码下载链接

    百度云盘链接,失效请留言 提取码:9k8i
    CSDN下载链接,失效请留言





    以上,就是我写的关于Winform打印表格 / 输出到Excel打印的操作,以作记录

    展开全文
  • 1.在Excel中针对以下表格已经设置过自动调整行高,且编辑状态下展示正常: 2.在打印预览和打印纸质文件状态下,第五行内容并未显示出来(当前使用的字体为宋体,字号11号): 3.当前可以达到打印效果正常的...

    首先说明,目前还没找到完善的解决方案,可以通过以下方式暂时处理(虽然达不到预期效果o(╥﹏╥)o),欢迎大家评论区讨论完善。

    复现场景:

    1.在Excel中针对以下表格已经设置过自动调整行高,且编辑状态下展示正常:

    2.在打印预览和打印纸质文件状态下,第五行内容并未显示出来(当前使用的字体为宋体,字号11号):

    3.当前可以达到打印效果正常的方式:将表格中文字字体设置为“等线”,效果如下:

    展开全文
  • excel是一款非常好用的电子表格软件,我们经常使用它来记录我们生活中工作中的一些信息、一些数据,当我们整理一些数据信息的时候我们需要将其打印出来使用,这应该怎样操作呢?不用担心,一起来学习学习吧! 当然第...

    excel是一款非常好用的电子表格软件,我们经常使用它来记录我们生活中工作中的一些信息、一些数据,当我们整理一些数据信息的时候我们需要将其打印出来使用,这应该怎样操作呢?不用担心,一起来学习学习吧!

    当然第一步就是需要我们打开excel表格文档,进入到我们需要打印信息表格界面,我在这里用简单数据表格信息进行演示。
    如图:
    在这里插入图片描述

    然后我们在excel上方菜单工具栏当中点击“文件”。
    如图:
    在这里插入图片描述

    点击进入到文件操作窗口界面,在左侧选项中选择“打印”。
    如图:
    在这里插入图片描述

    然后我们就进入到打印的编辑界面了,在打印界面可以选择打印机、设置打印份数、以及打印文档格式、纸张大小、边距等等设置,按照自己所需求的打印方式进行设置。
    如图:
    在这里插入图片描述

    当我们设置好打印的格式,确认完毕好可以打印了,然后我们点击上方的“打印”按钮即可完成打印。
    如图:
    在这里插入图片描述

    完成以上步骤我们就可以把我们表格文档中的内容进行打印出来了,很是简单且方便快捷,希望能够帮助到大家,感谢大家的观看。

    展开全文
  • 连接数据库,用QTableModel显示数据库的表格,然后将表格转为html,然后再进行打印打印的方法是参考以下链接完成的: Qt中(HTML)实现数据表格打印功能 输出 主界面 打印结果 代码 qprintermanager.h #ifndef ...
  • EXCEl有自动调整行高的功能,但是自动调整的行高十份紧凑,打印时会出现部分行的字下半截看不见的问题。 行不多时,可以手动调整行高,但行数超过百行后,手动工作量太大, 现使用VBA的方式进行批量调整行高,对微软...
  • 网上下载的pdf表格文档有时是被缩小且不居中的,打印的时候想要填充整个页面,操作步骤如下:
  • vue+element实现可编辑表格

    千次阅读 2020-06-05 03:46:32
    目录前言正文1.简单实现2.支持后台传输数据(ps:对删除和增加基本没影响,就不做展示)3....在我们一想到如何实现可编辑表格,首先想到的肯定是在表格最后加一个操作栏,来修改表格当前栏的内容形式,而可编辑这一
  • jQuery实现可以编辑表格实例详解

    千次阅读 2017-10-13 15:37:03
    效果图 - 点击单个可以进行修改 - 点击修改所有的表格都可以进行修改 - 点击保存所有的数据可以获取并打印出来 - 本人demo需要引入jq文件代码见如下(有详细的注解)<!... 编辑 <script src="j
  • 在PHP中创建和编辑Excel电子表格

    千次阅读 2018-09-12 23:17:33
    要使用纯PHP创建或编辑Excel电子表格,我们将使用PHPExcel库,它可以读写许多电子表格格式,包括xls,xlsx,ods和csv。在我们继续之前,仔细检查您的服务器上是否有PHP 5.2或更高版本以及安装了以下PHP扩展:php_zip...
  • 关于KindEditor 编辑器中表格的问题

    千次阅读 2019-05-06 17:23:32
    #关于KindEditor 编辑器中表格的问题 今天开发项目中有一个需求,需要多个表格联合打印,但是多个表格中又需要有各自独立的表头,起初是直接调用浏览器的打印接口,但是出现的问题是,当需要打印表格数据多时遇到...
  • 纯绿色web打印编辑器,支持自定义数据源、自定义模板、自定义打印的所有内容。可运用于快递单打印、发货单打印、报表打印等。
  • vue + elementUi实现可编辑表格数据

    千次阅读 2020-03-16 11:19:46
    由于项目需求 需要表格的内容可以进行修改 思路:当点击修改的时候切换到input输入框,否则就是文本 如图: 先定义一个变量用来控制span和input的显示隐藏,由于点击编辑只修改当行数据,所以用index索引来...
  • 声明:本文出自云上科技电脑工作室,版权所有,未经授权禁止转载,请尊重...下面笔者就跟大家来讲讲如何利用这两个工具来为Excel电子表格添加快速打印的功能按钮,只要轻轻点击一下这个按钮,内容就会发送至指定的...
  • 修改静态的表格过后,搞个触发事件打印出table中的缓存,建议先打印table查看自己的table结构 不同的版本结构会不一样 我使用的是 var table; function tableData() { console.log(table.cache.JxDataTabel);//...
  • Python xlrd 模块编辑 Excle 表格——基础篇2 冻结窗口、设置加密保护、打印设置等 前言:1、设置冻结窗口2、sheet表 前言: 上一篇博客对Python 的第三方模块 xlrd 对Excel表格的一些基础操作如:新建工作簿、增加...
  • 页面表格编辑input框变大的问题

    千次阅读 2017-09-17 15:05:51
    1、在页面添加表格表头 table width="100%" border="1" id="damageTab"> tr> td width="10px" align="center">序号td> td width="100px" align="center">车损详情td> td width="20px" ...
  • Table表格部分 &amp;lt;template&amp;gt; &amp;lt;el-table :data=&quot;tableData&quot; style=&quot;width: 100%&quot;&amp;gt; &amp;lt;el-table-column label=&...
  • web前端二维码打印模板编辑

    千次阅读 2019-09-07 15:56:01
    基于hiprint开源插件修改而来,实现了二维码模板在线编辑、保存、预览、打印,批量打印、自动分页 官网地址:http://www.hinnn.com/ 话不多说 直接从官网拿下来基本没问题(有问题也没办法,像我这直接把官网例子...
  • Word中表格编辑等技巧

    千次阅读 2013-11-20 17:00:13
    将插入点定位在单元格中,单击“表格→绘制斜线表头”命令,出现对话框,单击“表头样式”下拉菜单选择一种样式(共有5种可选择),分别填入“行标 题”(右上角的项目)、“列标题”(左下角的项目)和“数据标题”...
  • PHP+AJAX 实现表格实时编辑

    千次阅读 2018-08-02 18:02:44
    编辑表格DEMO title > < link rel = "stylesheet" href = "css/main.css" > head > < body > < div class = "container" > < table class = "data" > < tr > < td > 1 td > ...
  • 觉得 wiki下(mediawiki)导入excel和word表格好麻烦,微软
  • Openmeetings提供视频会议,即时通讯,白板,协同文档编辑和使用的Red5流媒体服务器的远程处理和流媒体API函数等群件工具。 007.tomcat http://tomcat.apache.org/ Apache Tomcat是一个实现了Java Servlet...
  • 迅捷CAD编辑器是一款专门为从事建筑设计和机械设计等相关的工作人员量身设计的一款CAD图纸查看、编辑、批量打印和批量转换的非常有用的办公软件。而且,迅捷CAD编辑器还兼容各个版本的DWG、DWS、DWT、DXF等多种常见...
  • 先说bootstrap-table插件:这个插件是一个bootstrap的一个表格插件,有分页和打印功能(打印功能没尝试)。 x-editable插件:表格编辑插件。 这两个插件分别提供了对方的js和css文件。 于是,感觉超级搭有木有...
  • 1. 首先最基本的,vue elementUI根据数据生成固定表项的表格 从官网摘个Demo过来: <el-table :data="tableData" style="width: 100%"> <el-table-column prop="date" label="日期" width="180"></...
  • JS中添加如下代码 别忘记表格绑定事件 index.html 中需要的添加如下代码 控制器中添加对应方法 $params 打印的就是你html中的数据
  • ExtJS4.2学习(七)EditorGrid可编辑表格

    千次阅读 2014-08-12 16:21:42
    编辑表格--EditorGrid 大家使用过Mircrosoft Excel,它的功能强大,用户可随意添加或删除表格中的行和列,而且只保存一此即可。EditorGrid也提供这些功能,可以直接在表格里执行添加、删除、修改和查找等操作...
  • 数据编辑操作 1.填充数据 填充数据见上一节 自动化办公之EXCEL教程(2):各种数据的输入,自动填充数据,表格操作小技巧 2.查找和替换数据 第一步:开始选项卡里找到查找 或者Ctil+F 第二步: 更多点击选项 左边...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 41,054
精华内容 16,421
关键字:

如何编辑打印表格