zoukankan      html  css  js  c++  java
  • Excel设置详细

    using System;  
    using System.Collections.Generic;  
    using System.Text;  
    using System.Reflection;  
    using System.Runtime.InteropServices;  
    using Microsoft.Office.Interop.Excel;  
    using ExcelApplication = Microsoft.Office.Interop.Excel.ApplicationClass;  
     
    namespace ExcalDemo  
    {  
        public class ExcelFiles  
        {  
            public void CreateExcelFiles()  
            {  
                //创建一个 Excel 实例  
                ExcelApplication excel = new ExcelApplication();  
                try 
                {  
                    // 注释掉的语句是:从磁盘指定位置打开一个 Excel 文件  
                    //excel.Workbooks.Open("demo.xls", Missing.Value, Missing.Value,   
            //Missing.Value,Missing.Value, Missing.Value, Missing.Value,   
            //Missing.Value, Missing.Value, Missing.Value, Missing.Value,   
            //Missing.Value, Missing.Value, Missing.Value, Missing.Value);  
                      
                    excel.Visible = false;// 不显示 Excel 文件,如果为 true 则显示 Excel 文件  
                    excel.Workbooks.Add(Missing.Value);// 添加工作簿  
                    Worksheet sheet = (Worksheet)excel.ActiveSheet;// 获取当前工作表  
     
                    Range range = null;// 创建一个空的单元格对象  
                      
                    range = sheet.get_Range("A1", Missing.Value);// 获取单个单元格  
                    range.RowHeight = 20;           // 设置行高  
                    range.ColumnWidth = 20;         // 设置列宽  
                    range.Borders.LineStyle = 1;    // 设置单元格边框  
                    range.Font.Bold = true;         // 加粗字体  
                    range.Font.Size = 20;           // 设置字体大小  
                    range.Font.ColorIndex = 5;      // 设置字体颜色  
                    range.Interior.ColorIndex = 6;  // 设置单元格背景色  
                    range.HorizontalAlignment = XlHAlign.xlHAlignCenter;// 设置单元格水平居中  
                    range.VerticalAlignment = XlVAlign.xlVAlignCenter;// 设置单元格垂直居中  
                    range.Value2 = "设置行高和列宽";// 设置单元格的值  
     
                    range = sheet.get_Range("B2", "D4");// 获取多个单元格  
                    range.Merge(Missing.Value);         // 合并单元格  
                    range.Columns.AutoFit();            // 设置列宽为自动适应  
                    range.NumberFormatLocal = "#,##0.00";// 设置单元格格式为货币格式  
            // 设置单元格左边框加粗  
                    range.Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;  
            // 设置单元格右边框加粗  
                    range.Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;  
                    range.Value2 = "合并单元格";  
     
                    // 页面设置  
                    sheet.PageSetup.PaperSize = XlPaperSize.xlPaperA4;          // 设置页面大小为A4  
                    sheet.PageSetup.Orientation = XlPageOrientation.xlPortrait; // 设置垂直版面  
                    sheet.PageSetup.HeaderMargin = 0.0;                         // 设置页眉边距  
                    sheet.PageSetup.FooterMargin = 0.0;                         // 设置页脚边距  
                    sheet.PageSetup.LeftMargin = excel.InchesToPoints(0.354330708661417); // 设置左边距  
                    sheet.PageSetup.RightMargin = excel.InchesToPoints(0.354330708661417);// 设置右边距  
                    sheet.PageSetup.TopMargin = excel.InchesToPoints(0.393700787401575);  // 设置上边距  
                    sheet.PageSetup.BottomMargin = excel.InchesToPoints(0.393700787401575);// 设置下边距  
                    sheet.PageSetup.CenterHorizontally = true;                  // 设置水平居中  
     
                    // 打印文件  
                    sheet.PrintOut(Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);  
                      
                    // 保存文件到程序运行目录下  
                    sheet.SaveAs(System.Windows.Forms.Application.StartupPath + "\demo.xls", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);  
     
                    excel.ActiveWorkbook.Close(false, null, null); // 关闭 Excel 文件且不保存  
                }  
                catch (Exception ex)  
                {  
                    MessageBox.Show(ex.Message);  
                }  
                finally 
                {  
                    excel.Quit(); // 退出 Excel  
                    excel = null; // 将 Excel 实例设置为空  
                }  
            }  
        }  

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using Microsoft.Office.Interop.Excel;
    using ExcelApplication = Microsoft.Office.Interop.Excel.ApplicationClass;

    namespace ExcalDemo
    {
        public class ExcelFiles
        {
            public void CreateExcelFiles()
            {
                //创建一个 Excel 实例
                ExcelApplication excel = new ExcelApplication();
                try
                {
                    // 注释掉的语句是:从磁盘指定位置打开一个 Excel 文件
                    //excel.Workbooks.Open("demo.xls", Missing.Value, Missing.Value,
            //Missing.Value,Missing.Value, Missing.Value, Missing.Value,
            //Missing.Value, Missing.Value, Missing.Value, Missing.Value,
            //Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                   
                    excel.Visible = false;// 不显示 Excel 文件,如果为 true 则显示 Excel 文件
                    excel.Workbooks.Add(Missing.Value);// 添加工作簿
                    Worksheet sheet = (Worksheet)excel.ActiveSheet;// 获取当前工作表

                    Range range = null;// 创建一个空的单元格对象
                   
                    range = sheet.get_Range("A1", Missing.Value);// 获取单个单元格
                    range.RowHeight = 20;           // 设置行高
                    range.ColumnWidth = 20;         // 设置列宽
                    range.Borders.LineStyle = 1;    // 设置单元格边框
                    range.Font.Bold = true;         // 加粗字体
                    range.Font.Size = 20;           // 设置字体大小
                    range.Font.ColorIndex = 5;      // 设置字体颜色
                    range.Interior.ColorIndex = 6;  // 设置单元格背景色
                    range.HorizontalAlignment = XlHAlign.xlHAlignCenter;// 设置单元格水平居中
                    range.VerticalAlignment = XlVAlign.xlVAlignCenter;// 设置单元格垂直居中
                    range.Value2 = "设置行高和列宽";// 设置单元格的值

                    range = sheet.get_Range("B2", "D4");// 获取多个单元格
                    range.Merge(Missing.Value);         // 合并单元格
                    range.Columns.AutoFit();            // 设置列宽为自动适应
                    range.NumberFormatLocal = "#,##0.00";// 设置单元格格式为货币格式
            // 设置单元格左边框加粗
                    range.Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;
            // 设置单元格右边框加粗
                    range.Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;
                    range.Value2 = "合并单元格";

                    // 页面设置
                    sheet.PageSetup.PaperSize = XlPaperSize.xlPaperA4;          // 设置页面大小为A4
                    sheet.PageSetup.Orientation = XlPageOrientation.xlPortrait; // 设置垂直版面
                    sheet.PageSetup.HeaderMargin = 0.0;                         // 设置页眉边距
                    sheet.PageSetup.FooterMargin = 0.0;                         // 设置页脚边距
                    sheet.PageSetup.LeftMargin = excel.InchesToPoints(0.354330708661417); // 设置左边距
                    sheet.PageSetup.RightMargin = excel.InchesToPoints(0.354330708661417);// 设置右边距
                    sheet.PageSetup.TopMargin = excel.InchesToPoints(0.393700787401575);  // 设置上边距
                    sheet.PageSetup.BottomMargin = excel.InchesToPoints(0.393700787401575);// 设置下边距
                    sheet.PageSetup.CenterHorizontally = true;                  // 设置水平居中

                    // 打印文件
                    sheet.PrintOut(Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                   
                    // 保存文件到程序运行目录下
                    sheet.SaveAs(System.Windows.Forms.Application.StartupPath + "\demo.xls", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

                    excel.ActiveWorkbook.Close(false, null, null); // 关闭 Excel 文件且不保存
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    excel.Quit(); // 退出 Excel
                    excel = null; // 将 Excel 实例设置为空
                }
            }
        }
    }

    作者:wpf之家
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    将python对象序列化成php能读取的格式(即能反序列化到对象)
    Atitit.研发管理---api版本号策略与版本控制
    Atitit.研发管理---api版本号策略与版本控制
    Atitit.jsou html转换纯文本 java c# php
    Atitit.jsou html转换纯文本 java c# php
    atitit.基于bat cli的插件管理系统.doc
    atitit.基于bat cli的插件管理系统.doc
    atititi.soa  微服务 区别 联系 优缺点.doc
    atititi.soa  微服务 区别 联系 优缺点.doc
    userService 用户 会员 系统设计 v2 q224 .doc
  • 原文地址:https://www.cnblogs.com/wpf123/p/2052951.html
Copyright © 2011-2022 走看看