EXCEL各种功能大集合

作者: new_blash | 来源:发表于2019-08-06 17:13 被阅读0次

    本人在项目中用到的一些简单的方法:

    using GN_Excel = Microsoft.Office.Interop.Excel;
    //首先声明需要用的功能
    public GN_Excel.Application app;
    public GN_Excel.Workbooks wbs;
    public GN_Excel.Workbook wb;
    public GN_Excel.Worksheet ws;
    //在方法中实例化
    app = new GN_Excel.Application();
    wbs = app.Workbooks;
    //打开excel文件
    wb = wbs.Open(path);//path是你excel文件的完整路径
    //获取sheet的数量
    int ss = wb.Worksheets.Count;
    //实例化操作那个sheet
    ws = wb.Worksheets[a];//a代表获取的sheet的数量中的其中一个,从1开始
    //调整列宽
    ws.Columns["A:A", Type.Missing].ColumnWidth = 5;//调整列宽
    //调整行高
    ws.Rows["1:8", Type.Missing].RowHeight = 18;//1-8行行高18
    //文字根据列宽缩放
    ws.Cells[5, 1].ShrinkToFit = true;
    //根据某一行的值来查询有多少行
    int max = 9999;
    for (int b = 1; b <= max; b++)
    {
        object NullText = ws.Range["A" + b].Value2;
        if (NullText.ToString() == "*****")
        {
              max = b ;
              break;
         }
         else {
         }
    }
    //内容居左(居上下左右中只需要改属性就可以了)
    ws.Range["A9:J" + max].HorizontalAlignment = GN_Excel.Constants.xlLeft;//A9J9到最后的内容进行居左
    //打印相关叶
    ws.PageSetup.CenterFooter = "第&P页,共&N页";
    //打印据边excel采用的是英寸,所有设置的数值都需要/2.54
    ws.PageSetup.TopMargin = app.InchesToPoints(0.5 / 2.54);//顶部
    ws.PageSetup.FooterMargin = app.InchesToPoints(0 / 2.54);//页脚
    ws.PageSetup.LeftMargin = app.InchesToPoints(0.5 / 2.54);//左边
    ws.PageSetup.RightMargin = app.InchesToPoints(0.5 / 2.54);//右边
    ws.PageSetup.BottomMargin = app.InchesToPoints(0.5 / 2.54);//底部
    ws.PageSetup.HeaderMargin = app.InchesToPoints(0 / 2.54);//页眉
    ws.PageSetup.Zoom = 100;//打印缩放100%
    ws.PageSetup.PrintTitleRows = "$1:$8";//打印标题栏
    ws.PageSetup.CenterHorizontally = true;//水平居中
    ws.PageSetup.CenterVertically = false;//垂直居中
    ws.PageSetup.PrintGridlines = true;//打印网格线
    //保存
    wb.Save();//保存EXCEL
    //另存为.xlsx(需要路径,文件名,后缀)
    wb.SaveAs(path+ "name"+".xlsx", GN_Excel.XlFileFormat.xlOpenXMLWorkbook, "", "", Type.Missing, Type.Missing, GN_Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    

    未完待续.........

    相关文章

      网友评论

        本文标题:EXCEL各种功能大集合

        本文链接:https://www.haomeiwen.com/subject/rlywdctx.html