using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
// 引入OFFICE EXCEL命名空间
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Data;
using System.IO;
using System.Diagnostics;
using System.Collections;
using System.Runtime.InteropServices;
namespace TestLib
{
public class ExcelOperation
{
private DateTime beforeTime; //Excel启动之前时间
private DateTime afterTime; //Excel启动之后时间
Excel.Application myExcel = new Excel.Application();
Excel.Workbook myBook;
Excel.Worksheet mySheet1;
Excel.Range range;
Excel.Range range1;
Excel.Range range2;
Excel.TextBox textBox;
public int sheetCount = 1; //WorkSheet数量
private string sheetPrefixName = "页";
#region 公共属性
///
/// WorkSheet前缀名,比如:前缀名为“页”,那么WorkSheet名称依次为“页-1,页-2...”
///
public string SheetPrefixName
{
set { this.sheetPrefixName = value; }
}
///
/// WorkSheet数量
///
public int WorkSheetCount
{
get { return myBook.Sheets.Count; }
}
#endregion
public ExcelOperation()
{
}
public void InitExcel() //先初始化
{
// Excel.Application myExcel = new Excel.Application();
if (myExcel == null)
{
MessageBox.Show("无法创建Excel对象,可能您的电脑未安装Excel!");
}
MessageBox.Show("初始化成功", "提示");
myExcel.Quit();
GC.Collect();
return;
}
public void CreateReport( ) //建立文档//此时的Excel表格是一个空的表格,没有任何内容
{
// 2.创建文档
// strTemplate:模板文档全名
// sheetCount:创建文档所需要的页数
//myExcel.Application.Workbooks.Add(true);//引用工作薄
myBook = myExcel.Workbooks.Add(Missing.Value);
mySheet1 = (Excel.Worksheet)myBook.Worksheets[1];
mySheet1 = (Excel.Worksheet)myExcel.ActiveSheet;// 获取当前工作表
myExcel.Visible = true;//使Excel可视
myExcel.DisplayAlerts = true;
}
public void SetTitle(/*string TitString*/) //设置报表头
{
range = mySheet1.get_Range("A6", "D6");// 获取多个单元格
range.Merge(Missing.Value); // 合并单元格
range.Columns.AutoFit(); // 设置列宽为自动适应
range.NumberFormatLocal = "#,##0.00";// 设置单元格格式为货币格式
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.Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;
// 设置单元格右边框加粗
//range.Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;
range.Value2 = "合并单元格";
}
public void SetContent() //设置内容
{
//在命名空间"Excel"中,还定义了一个类"Cell",这个类所代表的就是Excel表格中的一个单元格。通过给"Cell"赋值,从而实现往Excel表格中输入相应的数据
//Excel.Application excel = new Excel.Application () ;
//excel.Application.Workbooks.Add(true);
//excel.Cells[ 5 , 5 ] ="First Row First Column";
//excel.Cells[ 5 , 6 ] ="First Row Second Column";
//excel.Cells[ 6 , 5 ] ="Second Row First Column";
//excel.Cells[ 5 , 5 ] ="Second Row Second Column";
//excel.Visible = true ;
range = mySheet1.get_Range("A1", Missing.Value);// 获取单个单元格
range.Columns.AutoFit();
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 = mySheet1.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 = "合并单元格";
}
public void PrintSet () //打印设置
{
//mySheet1.PageSetup.PaperSize = mySheet1.XLPaperSize.xlPaperA4; // 设置页面大小为A4
//mySheet1.PageSetup.Orientation = XlPageOrientation.xlPortrait; // 设置垂直版面
mySheet1.PageSetup.HeaderMargin = 0.0; // 设置页眉边距
mySheet1.PageSetup.FooterMargin = 0.0; // 设置页脚边距
mySheet1.PageSetup.LeftMargin = myExcel.InchesToPoints(0.354330708661417); // 设置左边距
mySheet1.PageSetup.RightMargin = myExcel.InchesToPoints(0.354330708661417);// 设置右边距
mySheet1.PageSetup.TopMargin = myExcel.InchesToPoints(0.393700787401575); // 设置上边距
mySheet1.PageSetup.BottomMargin = myExcel.InchesToPoints(0.393700787401575);// 设置下边距
mySheet1.PageSetup.CenterHorizontally = true; // 设置水平居中
}
public void InsertPicture() //插入图片
{
}
public void SetColumnWidth() //设置列宽
{
}
public void PastePicture() //粘贴图片
{
}
public void InsertPicture(string RangeName, string PicturePath)
{
public void QuitExcel() //退出excle并退出excle就进程
{
Excel.Application myExcel = new Excel.Application();
myExcel.Quit();
GC.Collect();
return;
}
}
}