文档章节

C# EXcel 打印

可达鸭眉头一皱
 可达鸭眉头一皱
发布于 2015/12/15 16:39
字数 658
阅读 13
收藏 0

 Microsoft.Office.Interop.Excel.Application appexcel = new Microsoft.Office.Interop.Excel.Application();

            SaveFileDialog savefiledialog = new SaveFileDialog();

            System.Reflection.Missing miss = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Excel.Workbook workbookdata;
            Microsoft.Office.Interop.Excel.Worksheet worksheetdata=null;




            //设置对象不可见

            appexcel.Visible = false;

            System.Globalization.CultureInfo currentci = System.Threading.Thread.CurrentThread.CurrentCulture;

            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-us");

            workbookdata = appexcel.Workbooks.Add(miss);
            Dictionary<string, string> dicData = new Dictionary<string, string>();
            Microsoft.Office.Interop.Excel.Range range1 = null;
            Microsoft.Office.Interop.Excel.Range rngfirst = null;
            Microsoft.Office.Interop.Excel.Range range2 = null;
            Microsoft.Office.Interop.Excel.Range range3 = null;
            Microsoft.Office.Interop.Excel.Range range4 = null;
            Microsoft.Office.Interop.Excel.Range range5 = null;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (!dicData.ContainsKey(dataGridView1.Rows[i].Cells["type"].Value.ToString().Trim() + "," + dataGridView1.Rows[i].Cells["IClot_no"].Value.ToString().Trim()))
                {
                    dicData.Add(dataGridView1.Rows[i].Cells["type"].Value.ToString().Trim() + "," + dataGridView1.Rows[i].Cells["IClot_no"].Value.ToString().Trim(), null);
                }

            }
            int xiangshu = 0;///GF5118M  GF3118M箱数
            foreach (string key in dicData.Keys)
            {
                worksheetdata = (Microsoft.Office.Interop.Excel.Worksheet)workbookdata.Worksheets.Add(miss, miss, miss, miss);///新增sheet
                worksheetdata.Name = key;

                List<Maticsoft.Model.Pmc_WorderFT> PMCList = PMC.GetModelList(" type='" + key.Split(',')[0] + "' and  IncomeLot_no='" + key.Split(',')[1] + "'");

                worksheetdata.get_Range("A1:H1").Merge(worksheetdata.get_Range("A1:H1").MergeCells);//合并单元格
              
                 rngfirst = (Microsoft.Office.Interop.Excel.Range)worksheetdata.Cells[1, 1];
                rngfirst.Font.Size = 25;

                rngfirst.Font.Name = "Arial Unicode MS";//设置单元格字体

                rngfirst.RowHeight = 40;
                rngfirst.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                //rngfirst.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
                rngfirst.Font.Bold = true;
                worksheetdata.Cells[1, 1] = "LY测试报告单";
                worksheetdata.Cells[2, 1] = "客户";
                worksheetdata.Cells[2, 2] = "产品型号";
                worksheetdata.Cells[2, 3] = "产品批次";
                worksheetdata.Cells[2, 4] = "程序名称";
                worksheetdata.Cells[2, 5] = "版本号";
                worksheetdata.Cells[2, 6] = "CheckSum";
                worksheetdata.Cells[2, 7] = "固件版本";
                worksheetdata.Cells[2, 8] = "上货日期";
                if (PMCList.Count > 0)
                {
                    worksheetdata.Cells[3, 1] = PMCList[0].cu_no.Trim();
                    worksheetdata.Cells[3, 2] = PMCList[0].type;
                    worksheetdata.Cells[3, 3] = PMCList[0].IncomeLot_no;
                    worksheetdata.Cells[3, 4] = PMCList[0].tver;
                    worksheetdata.Cells[3, 5] = PMCList[0].tver;
                    worksheetdata.Cells[3, 6] = PMCList[0].checksum;
                    worksheetdata.Cells[3, 7] = PMCList[0].gver;
                    worksheetdata.Cells[3, 8] = PMCList[0].wo_date;
                }

                worksheetdata.get_Range("A4:H4").Merge(worksheetdata.get_Range("A4:H4").MergeCells);
                worksheetdata.get_Range("E5:F5").Merge(worksheetdata.get_Range("E5:F5").MergeCells);
                worksheetdata.get_Range("G5:H5").Merge(worksheetdata.get_Range("G5:H5").MergeCells);
                worksheetdata.Cells[5, 1] = "箱号";
                worksheetdata.Cells[5, 2] = "序列号";
                worksheetdata.Cells[5, 3] = "Frame总条数";
                worksheetdata.Cells[5, 4] = "Total";
                worksheetdata.Cells[5, 5] = "PASS";
                worksheetdata.Cells[5, 7] = "Fail";
                int l = 0;
                for (int k = 0; k < dataGridView1.Rows.Count; k++)
                {

                    if (dataGridView1.Rows[k].Cells["type"].Value.ToString().Trim() + "," + dataGridView1.Rows[k].Cells["IClot_no"].Value.ToString().Trim() == key)
                    {
                        if (dataGridView1.Rows[k].Cells["type"].Value.ToString().Trim() == "GF5118M" || dataGridView1.Rows[k].Cells["type"].Value.ToString().Trim() == "GF3118M")
                        {
                            worksheetdata.Cells[6 + l, 1] = xiangshu + 1;
                            xiangshu++;
                        }else
                        {
                            worksheetdata.Cells[6 + l, 1] = l + 1;
                        }
                        
                        worksheetdata.Cells[6 + l, 2] = "B" + dataGridView1.Rows[k].Cells["Code"].Value.ToString().Trim().Split('B')[1];
                        worksheetdata.Cells[6 + l, 3] = (int.Parse(dataGridView1.Rows[k].Cells["OK"].Value.ToString().Trim()) + int.Parse(dataGridView1.Rows[k].Cells["NG"].Value.ToString().Trim())) / 90;
                        worksheetdata.Cells[6 + l, 4] = int.Parse(dataGridView1.Rows[k].Cells["OK"].Value.ToString().Trim()) + int.Parse(dataGridView1.Rows[k].Cells["NG"].Value.ToString().Trim());
                        worksheetdata.get_Range("E" +(6 + l) + ":F" + (6 + l) + "").Merge(worksheetdata.get_Range("E" + (6 + l) + ":F" + (6 + l) + "").MergeCells);///合并EF
          
                        worksheetdata.get_Range("G" + (6 + l)+ ":H" + (6 + l) + "").Merge(worksheetdata.get_Range("G" + (6 + l) + ":H" + (6 + l) + "").MergeCells);///合并GH
                        worksheetdata.Cells[6 + l, 5] = int.Parse(dataGridView1.Rows[k].Cells["OK"].Value.ToString().Trim());
                        worksheetdata.Cells[6 + l, 7] = int.Parse(dataGridView1.Rows[k].Cells["NG"].Value.ToString().Trim());
                        l++;
                       
                       
                    }
                }
                int shu = 0;
                shu = l +5;
                 range1 = worksheetdata.get_Range("A5", "H" + shu);
                range1.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
                range1.RowHeight = 22;///设置高度
                range1.ColumnWidth = 17;///设置宽度

                //range1.EntireColumn.AutoFit();
                range1.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
              
                range2 = worksheetdata.get_Range("A2", "H5" );
                range2.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
                range2.RowHeight = 28;
                range2.ColumnWidth = 17;
                //range2.EntireColumn.AutoFit();
                range2.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                range3 = worksheetdata.get_Range("A2", "A" + shu);
                range3.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
                range3.RowHeight = 28;
                range3.ColumnWidth = 8;
                //range3.EntireColumn.AutoFit();
                range3.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                range4 = worksheetdata.get_Range("A2", "H" + 2);
                range4.Font.Bold = true;
                range5 = worksheetdata.get_Range("A5", "H" + 5);
                range5.Font.Bold = true;
                worksheetdata.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape;///设置横向打印
            }

            appexcel.Visible = true;
            workbookdata.Close();
            appexcel.Quit();
           
            // 9.释放资源  
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rngfirst);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(range1);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(range2);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(range3);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(range4);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(range5);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheetdata);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(workbookdata);

            System.Runtime.InteropServices.Marshal.ReleaseComObject(appexcel);

            // 10.调用GC的垃圾收集方法  
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
    }

© 著作权归作者所有

共有 人打赏支持
可达鸭眉头一皱
粉丝 3
博文 75
码字总数 33248
作品 0
广州
私信 提问
C# 插入、删除Excel分页符

概述 对Excel表格设置分页对我们预览、打印文档时是很方便的,特别是一些包含很多复杂数据的、不规则的表格,为保证打印时每一页的排版美观性或者数据的前后连接的完整性,此时的分页符就发挥...

E_iceblue
2018/05/18
0
0
Excel、Exchange和C#

摘要:Eric Gunnerson 将向您介绍如何使用 Outlook、Excel 和 C# 创建自定义的日历,该日历可以提供适用于短期项目和长期项目的清晰明了的版式。 下载 csharp05152003_sample.exe 示例文件(...

晨曦之光
2012/03/09
186
0
C# 插入Excel页眉、页脚

简介 我们可以通过代码编程来对Excel工作表实现很多操作,在下面的示例中,将介绍如何来添加Excel页眉、页脚。在页眉处,我们可以添加文字,如公司名称、页码、工作表名、日期等,也可以添加...

E_iceblue
2018/06/08
0
0
创建和管理Excel文件的控件Spire.XLS for .NET

Spire.XLS for .NET控件是e-iceblue公司开发的一款支持对所有Excel格式类型文件进行操作的.NET 控件。它适用于任何类型的应用程序比如ASP.NET Web应用程序或者Windows桌面应用程序。Spire.X...

netkongjian
2014/06/24
0
0
Spread for ASP.NET 在线信息填报系统演示

本示例使用了 Spread 模拟在线信息填报系统, 演示了在线提交订单、离线编辑报表模板以及按日期查看日报表、年报表及销售图表等功能。 在线信息填报系统演示 表格控件是最常用的数据处理控件...

葡萄城控件技术团队
2014/07/18
2.4K
0

没有更多内容

加载失败,请刷新页面

加载更多

【抢购/秒杀】redis实现高并发下的抢购/秒杀功能

问题: 抢购/秒杀是如今很常见的一个应用场景,那么高并发竞争下如何解决超抢(或超卖库存不足为负数的问题)呢? 常规写法: 查询出对应商品的库存,看是否大于0,然后执行生成订单等操作,但...

echojson
17分钟前
2
0
tomcat 集群与 redis 分布式

1.学习

狼王黄师傅
20分钟前
0
0
如何正确的每天坚持背5个单词?

自从开始学习英语,我们的老师就一直告诉我们,如果每天能坚持背五个单词,一年365天就能背1800个单词初中三年,你就能背会5400个单词,背会5400个单词,就算参加高考也搓搓有余。 好吧,话是...

我是菜鸟我骄傲
29分钟前
1
0
Docker之MySql5.7中only_full_group_by的问题解决

MySql的镜像,默认情况下,MySql5.7中的sql_mode含有only_full_group_by,group by语句有时候会报错。通过手动修改sql_mode,那么如果删除容器或者新建容器,就会导致我们手动设置的sql_mod...

克虏伯
46分钟前
0
0
介绍Python中6个序列的内置类型

1、Python中6个序列的内置类型分别是什么? Python包含6中内建的序列,即列表、元组、字符串、Unicode字符串、buffer对象和 xrange 对象。序列通用的操作包括:索引、长度、组合(序列相加)...

问题终结者
今天
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部