zoukankan      html  css  js  c++  java
  • Excel2

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Office.Interop;
    using Microsoft.Office.Core;
    using System.Data;
    using System.Configuration;
    using Microsoft.Office.Interop.Excel;

    namespace xlsCommon
    {
       public class ExcelEdit
        {
           public string mFileName;
           public Application app;
           public Workbooks wbs;
           public Workbook wb;
           public Worksheets wss;
           public Worksheet ws;

           public ExcelEdit()
           {
           }

           /// <summary>
           /// 创建一个Excel对象
           /// </summary>
           public void Create()
           {
               app = new Application();
               wbs = app.Workbooks;
               wb = wbs.Add(true);
           }

           /// <summary>
           /// 打开一个Excel文件
           /// </summary>
           /// <param name="FileName">文件名</param>
           public void Open(string FileName)
           {
               app = new Application();
               wbs = app.Workbooks;
               wb = wbs.Add(FileName);

               wbs.Open(FileName, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

               mFileName = FileName;
           }

           /// <summary>
           /// 获取一个工作表
           /// </summary>
           /// <param name="SheetName"></param>
           /// <returns></returns>
           public Worksheet GetSheet(string SheetName)
           {
               Worksheet s = (Worksheet)wb.Worksheets[SheetName];
               return s;
           }

           /// <summary>
           /// 添加一个工作表
           /// </summary>
           /// <param name="SheetName"></param>
           /// <returns></returns>
           public Worksheet AddSheet(string SheetName)
           {
               Worksheet s = (Worksheet)wb.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
               s.Name = SheetName;
               return s;
           }

           /// <summary>
           /// 删除一个工作表
           /// </summary>
           /// <param name="SheetName"></param>
           public void DelSheet(string SheetName)
           {
               ((Worksheet)wb.Worksheets[SheetName]).Delete();
           }

           /// <summary>
           /// 重命名工作表
           /// </summary>
           /// <param name="OldSheetName"></param>
           /// <param name="NewSheetName"></param>
           /// <returns></returns>
           public Worksheet ReNameSheet(string OldSheetName, string NewSheetName)
           {
               Worksheet s = (Worksheet)wb.Worksheets[OldSheetName];
               s.Name = NewSheetName;
               return s;
           }

           public Worksheet ReNameSheet(Worksheet Sheet, string NewSheetName)
           {
               Sheet.Name = NewSheetName;
               return Sheet;
           }

           public void SetCellValue(Worksheet ws, int x, int y, object value)
           {
               ws.Cells[x, y] = value;
           }

           public void SetCellValue(string ws, int x, int y, object value)
           {
               GetSheet(ws).Cells[x, y] = value;
           }

           public void SetCellProperty(Worksheet ws, int Startx, int Starty, int Endx, int Endy, int size, string name, Constants color, Constants HorizontalAlignment)
           {
               name = "宋体";
               size = 12;
               color = Constants.xlAutomatic;
               HorizontalAlignment = Constants.xlRight;
               ws.get_Range(ws.Cells[Startx,Starty],ws.Cells[Endx,Endy]).Font.Name = name;
               ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Size = size;
               ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Color = color;
               ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).HorizontalAlignment = HorizontalAlignment;
           }

           public void SetCellProperty(string wsn, int Startx, int Starty, int Endx, int Endy, int size, string name, Constants color, Constants HorizontalAlignment)
           {
               Worksheet ws = GetSheet(wsn);
               ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Name = name;
               ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Size = size;
               ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Color = color;
               ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).HorizontalAlignment = HorizontalAlignment;
           }

           /// <summary>
           /// 合并单元格
           /// </summary>
           /// <param name="ws"></param>
           /// <param name="x1"></param>
           /// <param name="y1"></param>
           /// <param name="x2"></param>
           /// <param name="y2"></param>
           public void UniteCells(Worksheet ws, int x1, int y1, int x2, int y2)
           {
               ws.get_Range(ws.Cells[x1, y1], ws.Cells[x2, y2]).Merge(Type.Missing);
           }

           /// <summary>
           /// 合并单元格
           /// </summary>
           /// <param name="ws"></param>
           /// <param name="x1"></param>
           /// <param name="y1"></param>
           /// <param name="x2"></param>
           /// <param name="y2"></param>
           public void UniteCells(string ws, int x1, int y1, int x2, int y2)
           {
               GetSheet(ws).get_Range(GetSheet(ws).Cells[x1, y1], GetSheet(ws).Cells[x2, y2]).Merge(Type.Missing);
           }

           /// <summary>
           /// 将数据插入到Excel的指定工作表的指定位置
           /// 使用模板时使用(一)
           /// </summary>
           /// <param name="dt"></param>
           /// <param name="ws"></param>
           /// <param name="startX"></param>
           /// <param name="startY"></param>
           public void InsertTable(System.Data.DataTable  dt, string ws, int startX, int startY)
           {
               for (int i = 0; i <= dt.Rows.Count - 1; i++)
               {
                   for (int j = 0; j <= dt.Columns.Count - 1; j++)
                   {
                       GetSheet(ws).Cells[startX + i, j + startY] = dt.Rows[i][j].ToString();
                   }
               }
           }

           /// <summary>
           /// 将数据插入到Excel指定工作表的指定位置(二)
           /// </summary>
           /// <param name="dt"></param>
           /// <param name="ws"></param>
           /// <param name="startX"></param>
           /// <param name="startY"></param>
           public void InsertTable(System.Data.DataTable dt, Worksheet ws, int startX, int startY)
           {
               for (int i = 0; i <= dt.Rows.Count - 1; i++)
               {
                   for (int j = 0; j <= dt.Columns.Count - 1; j++)
                   {
                       ws.Cells[startX + i, j + startY] = dt.Rows[i][j];
                   }
               }
           }

           /// <summary>
           /// 将数据添加到Excel的指定位置(一)
           /// </summary>
           /// <param name="dt"></param>
           /// <param name="ws"></param>
           /// <param name="startX"></param>
           /// <param name="startY"></param>
           public void AddTable(System.Data.DataTable dt, string ws, int startX, int startY)
           {
               for (int i = 0; i <= dt.Rows.Count - 1; i++)
               {
                   for (int j = 0; j <= dt.Columns.Count - 1; j++)
                   {
                       GetSheet(ws).Cells[i + startX, j + startY] = dt.Rows[i][j];
                   }
               }
           }

           /// <summary>
           ///  将数据添加到Excel的指定位置(二)
           /// </summary>
           /// <param name="dt"></param>
           /// <param name="ws"></param>
           /// <param name="startX"></param>
           /// <param name="startY"></param>
           public void AddTable(System.Data.DataTable dt, Worksheet ws, int startX, int startY)
           {
               for (int i = 0; i <= dt.Rows.Count - 1; i++)
               {
                   for (int j = 0; j <= dt.Columns.Count - 1; j++)
                   {
                       ws.Cells[i + startX, j + startY] = dt.Rows[i][j];
                   }
               }
           }

           /// <summary>
           /// 插入图片(一)
           /// </summary>
           /// <param name="Filename"></param>
           /// <param name="ws"></param>
           public void InsertPictures(string Filename, string ws)
           {
               GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoCTrue, 10, 10, 150, 150);
           }

           /// <summary>
           /// 插入图表
           /// </summary>
           /// <param name="ChartType"></param>
           /// <param name="ws"></param>
           /// <param name="DataSourcesX1"></param>
           /// <param name="DataSourceY1"></param>
           /// <param name="DataSourcesX2"></param>
           /// <param name="DataSourcesY2"></param>
           /// <param name="CharDataType"></param>
           public void InsertActiveChart(XlChartType ChartType, string ws, int DataSourcesX1, int DataSourceY1, int DataSourcesX2, int DataSourcesY2, Microsoft.Office.Interop.Excel.XlRowCol ChartDataType)
           {
               ChartDataType = Microsoft.Office.Interop.Excel.XlRowCol.xlColumns;
               wb.Charts.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
               {
                   wb.ActiveChart.ChartType = ChartType;
                   wb.ActiveChart.SetSourceData(GetSheet(ws).get_Range(GetSheet(ws).Cells[DataSourcesX1, DataSourcesY1], GetSheet(ws).Cells[DataSourcesX2, DataSourcesY2]), ChartDataType);
                   wb.ActiveChart.Location(XlChartLocation.xlLocationAsObject, ws);
               }
           }

           public bool Save()
           {
               if (mFileName == "")
               {
                   return false;
               }
               else
               {
                   try
                   {
                       wb.Save();
                       return true;
                   }
                   catch (Exception ex)
                   {
                       return false;
                   }
               }
           }

           public bool SaveAs(object FileName)
           {
               try
               {
                   wb.SaveAs(FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                      XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                   return true;
               }
               catch (Exception ex)
               {
                   return false;
               }
           }

           public void Close()
           {
               wb.Close(Type.Missing, Type.Missing, Type.Missing);
               wbs.Close();
               app.Quit();
               wb = null;
               wbs = null;
               app = null;
               GC.Collect();
           }
        }
    }

  • 相关阅读:
    灰度图像的直方图
    两幅图像的融合与叠加
    图像像素基本操作——自然系列滤镜
    图像像素加减乘除操作
    调整图像亮度、对比度、饱和度
    基于BufferedImage的图像滤镜演示
    Java——BufferedImage操作实例
    Java——BufferedImage对象
    Java——绘制五角星
    Java——自定义外部字体文件
  • 原文地址:https://www.cnblogs.com/daiweixm/p/1846648.html
Copyright © 2011-2022 走看看