zoukankan      html  css  js  c++  java
  • 将DataTable 导入Excel并打印

    using System;
    
    using System.Collections.Generic;
    
    using System.Text;
    
    using System.Data;
    
    
    
    namespace Personal
    
    {
    
        public class Print
    
        {
    
            /// <summary>
    
            /// 将DataTabl内容打印出来
    
            /// Table 表, strHead 表名,Zoom 缩放比%
    
            /// </summary>
    
            private void baobiao(DataTable Table, string strHead, sbyte Zoom)
    
            {
    
                try
    
                {
    
    
    
                    Excel.Application excelKccx = new Excel.Application();     //创建excel对象
    
                    Excel._Workbook xBk;
    
                    Excel._Worksheet xSt;
    
                    xBk = excelKccx.Workbooks.Add(true);
    
                    xSt = (Excel._Worksheet)xBk.ActiveSheet;
    
                    Excel.Range range = xSt.get_Range(excelKccx.Cells[1, 1], excelKccx.Cells[1, 22]);
    
                    range.ClearContents();//先把Range内容清除,合并才不会出错
    
                    range.MergeCells = true;
    
                    range.Value2 = strHead;//"普通医保住院定额超支管理报表(一般住院)";
    
                    range.Font.Bold = true;
    
                    range.Font.Size = 15;
    
                    range.Font.ColorIndex = 4;//颜色
    
                    range.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter; //设置标题格式为居中对齐 
    
                  //  设置表头 
    
                    int row = 2;
    
                    for (int i = 0; i < Table.Columns.Count; i++)//取字段名
    
                    {
    
                        excelKccx.Cells[row, i + 1] = Table.Columns[i].ColumnName.ToString();
    
                        xSt.get_Range(excelKccx.Cells[row, i + 1], excelKccx.Cells[row, i + 1]).Interior.ColorIndex = 5;
    
                    }
    
    
    
                    xSt.get_Range(excelKccx.Cells[row, 1], excelKccx.Cells[row, Table.Columns.Count]).EntireColumn.AutoFit();//自动调整列宽
    
                    xSt.get_Range(excelKccx.Cells[row, 1], excelKccx.Cells[row, Table.Columns.Count]).Font.Bold = true;
    
    
    
                    for (int i = 0; i < Table.Rows.Count; i++)//取记录值
    
                    {
    
                        row++;
    
                        for (int j = 0; j < Table.Columns.Count; j++)
    
                        {
    
                            excelKccx.Cells[row, j + 1] = Table.Rows[i][j].ToString();
    
                        }
    
                    }
    
    
    
    
    
                    excelKccx.Visible = true;
    
                    object oMissing = System.Reflection.Missing.Value;
    
                    xSt.PageSetup.PaperSize = Excel.XlPaperSize.xlPaperA3;//设置纸张大小
    
                    xSt.PageSetup.Zoom = Zoom; //缩放比例
    
                    xSt.PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape;//横向打印    xlPortrait1-纵向,2-横向;  
    
                    xSt.PrintOut(oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
    
    
    
                    xBk.PrintOut(oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing); // 打印:
    
                   // xBk.PrintPreview(oMissing);//打印预览
    
                    xBk.Close(null, null, null);
    
    
    
                    excelKccx.Workbooks.Close();
    
                    excelKccx.Application.Quit();
    
                    excelKccx.Quit();
    
    
    
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(excelKccx);   
    
                    GC.Collect();//强行销毁
    
                }
    
                catch
    
                {
    
                    GC.Collect();//强行销毁
    
                };
    
    
    
            }
    
        }
    
    }
    
    
  • 相关阅读:
    hadoop项目放在tomcat服务器中遇见的问题
    hadoop-hdfs间文件复制
    fastdfs扩容
    mysql-8.0修改密码方式
    同一个服务器启动两个redis服务记录!
    springboot+vue完美跨域 解决sessionId不一致问题
    docker 拉取fastDFS镜像
    共享锁、排他锁、互斥锁、悲观锁、乐观锁、行锁、表锁、页面锁、不可重复读、丢失修改、读脏数据
    linux下后台运行node-js项目
    概念解释:分组密码、流密码、对称密码、非对称密码
  • 原文地址:https://www.cnblogs.com/luluping/p/1429860.html
Copyright © 2011-2022 走看看