1559 lines
		
	
	
		
			54 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			1559 lines
		
	
	
		
			54 KiB
		
	
	
	
		
			C#
		
	
	
	
| using NPOI.HSSF.UserModel;
 | ||
| using NPOI.SS.UserModel;
 | ||
| using NPOI.SS.Util;
 | ||
| using NPOI.XSSF.UserModel;
 | ||
| using System;
 | ||
| using System.Collections.Generic;
 | ||
| using System.Data;
 | ||
| using System.Data.OleDb;
 | ||
| using System.IO;
 | ||
| using System.Linq;
 | ||
| using System.Text;
 | ||
| 
 | ||
| namespace BLL.Common
 | ||
| {
 | ||
|     public class NPOIExcel
 | ||
|     {
 | ||
|         internal IWorkbook Book { get; set; }
 | ||
| 
 | ||
|         private int sheetID = 0;
 | ||
|         /// <summary>
 | ||
|         /// 当前活动的SheetID,所有的操作将指向这个Sheet
 | ||
|         /// </summary>
 | ||
|         public int ActiveSheetID
 | ||
|         {
 | ||
|             get
 | ||
|             {
 | ||
|                 return sheetID;
 | ||
|             }
 | ||
|             set
 | ||
|             {
 | ||
|                 sheetID = value;
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 当前活动的SheetName,所有的操作将指向这个Sheet
 | ||
|         /// </summary>
 | ||
|         public String ActiveSheetName
 | ||
|         {
 | ||
|             get
 | ||
|             {
 | ||
|                 return Book.GetSheetAt(sheetID).SheetName;
 | ||
|             }
 | ||
|             set
 | ||
|             {
 | ||
|                 sheetID = Book.GetSheetIndex(value);
 | ||
|             }
 | ||
|         }
 | ||
|         /// <summary>
 | ||
|         /// 当前活动的Sheet,所有的操作将指向这个Sheet
 | ||
|         /// </summary>
 | ||
|         public ISheet ActiveSheet
 | ||
|         {
 | ||
|             get
 | ||
|             {
 | ||
|                 return Book.GetSheetAt(sheetID);
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 第一行非空行的行号
 | ||
|         /// </summary>
 | ||
|         public int FirstRowNum
 | ||
|         {
 | ||
|             get
 | ||
|             {
 | ||
|                 return Book.GetSheetAt(sheetID).FirstRowNum;
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 最后一行非空行的行号
 | ||
|         /// </summary>
 | ||
|         public int LastRostNum
 | ||
|         {
 | ||
|             get
 | ||
|             {
 | ||
|                 return Book.GetSheetAt(sheetID).LastRowNum;
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 无模板的Excel生成或操作
 | ||
|         /// </summary>
 | ||
|         public NPOIExcel()
 | ||
|         {
 | ||
|             Book = new HSSFWorkbook();
 | ||
|             Book.CreateSheet();
 | ||
|         }
 | ||
| 
 | ||
|         public NPOIExcel(Stream fileStream, String fileName)
 | ||
|         {
 | ||
|             if (fileName.Substring(fileName.LastIndexOf(".")) == ".xls")
 | ||
|             {
 | ||
|                 Book = new HSSFWorkbook(fileStream);
 | ||
|             }
 | ||
|             else
 | ||
|             {
 | ||
|                 Book = new XSSFWorkbook(fileStream);
 | ||
|             }
 | ||
|         }
 | ||
|         /// <summary>
 | ||
|         /// 带模板或数据的Excel生成或操作
 | ||
|         /// </summary>
 | ||
|         /// <param name="fileName"></param>
 | ||
|         public NPOIExcel(String fileName)
 | ||
|         {
 | ||
|             Book = CreateBook(fileName);
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 创建Excel Book
 | ||
|         /// </summary>
 | ||
|         /// <param name="fileName">模板文件名</param>
 | ||
|         /// <returns></returns>
 | ||
|         private IWorkbook CreateBook(String fileName)
 | ||
|         {
 | ||
|             FileInfo file = new FileInfo(fileName);
 | ||
|             if (!file.Exists)
 | ||
|             {
 | ||
|                 File.Create(fileName).Close();
 | ||
|             }
 | ||
|             FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
 | ||
|             IWorkbook book;
 | ||
|             if (file.Extension == ".xls")
 | ||
|             {
 | ||
|                 book = new HSSFWorkbook(fs);
 | ||
|             }
 | ||
|             else
 | ||
|             {
 | ||
|                 book = new XSSFWorkbook(fs);
 | ||
|             }
 | ||
|             fs.Close();
 | ||
|             if (book.NumberOfSheets == 0)
 | ||
|             {
 | ||
|                 book.CreateSheet();
 | ||
|             }
 | ||
|             return book;
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 新建Sheet
 | ||
|         /// </summary>
 | ||
|         /// <returns>新建Sheet</returns>
 | ||
|         public ISheet CreateSheet()
 | ||
|         {
 | ||
|             return Book.CreateSheet();
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 新建Sheet
 | ||
|         /// </summary>
 | ||
|         /// <param name="sheetName">新建Sheet的名称</param>
 | ||
|         /// <returns>新建Sheet</returns>
 | ||
|         public ISheet CreateSheet(String sheetName)
 | ||
|         {
 | ||
|             return Book.CreateSheet(sheetName);
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 设置行高
 | ||
|         /// 注:只对当前ActiveSheet有效
 | ||
|         /// </summary>
 | ||
|         /// <param name="rowIndex">行号</param>
 | ||
|         /// <param name="height">高度</param>
 | ||
|         public void SetRowHeight(int rowIndex, float height)
 | ||
|         {
 | ||
|             IRow row = Book.GetSheetAt(sheetID).GetRow(rowIndex);
 | ||
|             if (row == null)
 | ||
|             {
 | ||
|                 row = Book.GetSheetAt(sheetID).CreateRow(rowIndex);
 | ||
|             }
 | ||
|             row.Height = (short)(height * 20);
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 设置列宽
 | ||
|         /// 注:只对当前ActiveSheet有效
 | ||
|         /// </summary>
 | ||
|         /// <param name="columnIndex">列号</param>
 | ||
|         /// <param name="width">宽度</param>
 | ||
|         public void SetColumnWidth(int columnIndex, short width)
 | ||
|         {
 | ||
|             Book.GetSheetAt(sheetID).SetColumnWidth(columnIndex, width * 256);
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 获取或设置默认行高
 | ||
|         /// 注:只对当前ActiveSheet有效
 | ||
|         /// </summary>
 | ||
|         public short DefaultRowHeight
 | ||
|         {
 | ||
|             get
 | ||
|             {
 | ||
|                 return (short)(Book.GetSheetAt(sheetID).DefaultRowHeight / 20);
 | ||
|             }
 | ||
|             set
 | ||
|             {
 | ||
|                 Book.GetSheetAt(sheetID).DefaultRowHeight = (short)(value * 20);
 | ||
| 
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 获取或设置默认列宽
 | ||
|         /// 注:只对当前ActiveSheet有效
 | ||
|         /// </summary>
 | ||
|         public int DefaultColWidth
 | ||
|         {
 | ||
|             get
 | ||
|             {
 | ||
|                 return Book.GetSheetAt(sheetID).DefaultColumnWidth;
 | ||
|             }
 | ||
|             set
 | ||
|             {
 | ||
|                 Book.GetSheetAt(sheetID).DefaultColumnWidth = value;
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 某一列的列宽自动调整大小
 | ||
|         /// 注:只对当前ActiveSheet有效
 | ||
|         /// </summary>
 | ||
|         /// <param name="colIndex">列号</param>
 | ||
|         public void AutoColWidth(int colIndex)
 | ||
|         {
 | ||
|             Book.GetSheetAt(sheetID).AutoSizeColumn(colIndex, true);
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 隐藏一行
 | ||
|         /// 注:只对当前ActiveSheet有效
 | ||
|         /// </summary>
 | ||
|         /// <param name="rowIndex">行号</param>
 | ||
|         public void HiddenRow(int rowIndex)
 | ||
|         {
 | ||
|             IRow row = Book.GetSheetAt(sheetID).GetRow(rowIndex);
 | ||
|             if (row == null)
 | ||
|             {
 | ||
|                 row = Book.GetSheetAt(sheetID).CreateRow(rowIndex);
 | ||
|             }
 | ||
|             row.ZeroHeight = true;
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 删除一行
 | ||
|         /// 注:只对当前ActiveSheet有效
 | ||
|         /// </summary>
 | ||
|         /// <param name="rowIndex">行号</param>
 | ||
|         public void RemoveRow(int rowIndex)
 | ||
|         {
 | ||
|             IRow row = Book.GetSheetAt(sheetID).GetRow(rowIndex);
 | ||
|             if (row != null)
 | ||
|             {
 | ||
|                 ActiveSheet.RemoveRow(row);
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 读取单元格的值
 | ||
|         /// 注:只对当前ActiveSheet有效
 | ||
|         /// </summary>
 | ||
|         /// <param name="rowIndex">行号</param>
 | ||
|         /// <param name="columnIndex">列号</param>
 | ||
|         /// <returns>单元格的值</returns>
 | ||
|         public object ReadValue(int rowIndex, int columnIndex, bool? isDateTime = null)
 | ||
|         {
 | ||
|             try
 | ||
|             {
 | ||
|                 ICell cell = Book.GetSheetAt(sheetID).GetRow(rowIndex).GetCell(columnIndex);
 | ||
|                 short df = cell.CellStyle.DataFormat;
 | ||
| 
 | ||
|                 //return cell.ToString();
 | ||
|                 switch (cell.CellType)
 | ||
|                 {
 | ||
|                     case CellType.Blank:
 | ||
|                         return null;
 | ||
|                     case CellType.Boolean:
 | ||
|                         return cell.BooleanCellValue;
 | ||
|                     case CellType.Error:
 | ||
|                         throw new Exception("Cell Value Error");
 | ||
|                     case CellType.Formula:
 | ||
|                         {
 | ||
|                             switch (cell.CachedFormulaResultType)
 | ||
|                             {
 | ||
|                                 case CellType.Blank:
 | ||
|                                     return "";
 | ||
|                                 case CellType.Boolean:
 | ||
|                                     return cell.BooleanCellValue;
 | ||
|                                 case CellType.Error:
 | ||
|                                     throw new Exception("Cell Value Error");
 | ||
|                                 case CellType.Formula:
 | ||
|                                     throw new Exception("The Formula of this cell is too complex!");
 | ||
|                                 case CellType.Numeric:
 | ||
|                                     if (isDateTime == null)
 | ||
|                                     {
 | ||
|                                         if (DateUtil.IsCellDateFormatted(cell))
 | ||
|                                         {
 | ||
|                                             return cell.DateCellValue;
 | ||
|                                         }
 | ||
|                                         else
 | ||
|                                         {
 | ||
|                                             return cell.NumericCellValue;
 | ||
|                                         }
 | ||
|                                     }
 | ||
|                                     else if (isDateTime == true)
 | ||
|                                     {
 | ||
|                                         return cell.DateCellValue;
 | ||
|                                     }
 | ||
|                                     else
 | ||
|                                     {
 | ||
|                                         return cell.NumericCellValue;
 | ||
|                                     }
 | ||
|                                 case CellType.String:
 | ||
|                                     return cell.StringCellValue;
 | ||
|                                 case CellType.Unknown:
 | ||
|                                     return cell.ToString();
 | ||
|                                 default:
 | ||
|                                     return cell.ToString();
 | ||
|                             }
 | ||
|                         }
 | ||
|                     case CellType.Numeric:
 | ||
|                         {
 | ||
|                             if (isDateTime == null)
 | ||
|                             {
 | ||
|                                 if (DateUtil.IsCellDateFormatted(cell))
 | ||
|                                 {
 | ||
|                                     return cell.DateCellValue;
 | ||
|                                 }
 | ||
|                                 else
 | ||
|                                 {
 | ||
|                                     return cell.NumericCellValue;
 | ||
|                                 }
 | ||
|                             }
 | ||
|                             else if (isDateTime == true)
 | ||
|                             {
 | ||
|                                 return cell.DateCellValue;
 | ||
|                             }
 | ||
|                             else
 | ||
|                             {
 | ||
|                                 return cell.NumericCellValue;
 | ||
|                             }
 | ||
|                         }
 | ||
|                     case CellType.String:
 | ||
|                         return cell.StringCellValue;
 | ||
|                     case CellType.Unknown:
 | ||
|                         return cell.ToString();
 | ||
|                     default:
 | ||
|                         return cell.ToString();
 | ||
|                 }
 | ||
|             }
 | ||
|             catch (System.NullReferenceException)
 | ||
|             {
 | ||
|                 return null;
 | ||
|             }
 | ||
|             catch (Exception ex)
 | ||
|             {
 | ||
|                 throw ex;
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 设置单元格的值
 | ||
|         /// 注:只对当前ActiveSheet有效
 | ||
|         /// </summary>
 | ||
|         /// <param name="rowIndex">行号</param>
 | ||
|         /// <param name="columnIndex">列号</param>
 | ||
|         /// <param name="value">单元格的值</param>
 | ||
|         public void SetValue(int rowIndex, int columnIndex, object value)
 | ||
|         {
 | ||
|             SetValue(rowIndex, columnIndex, value, false);
 | ||
|         }
 | ||
|         public void SetPicValue(int rowIndex, int columnIndex, string value)
 | ||
|         {
 | ||
|             byte[] bytes = System.IO.File.ReadAllBytes(value);
 | ||
|             int pictureIdx = Book.AddPicture(bytes, PictureType.JPEG);
 | ||
|              
 | ||
|             // 第三步:创建画部
 | ||
|             IDrawing patriarch = ActiveSheet.CreateDrawingPatriarch();
 | ||
|             // 第四步:设置锚点
 | ||
|             int rowline = 1; // y方向
 | ||
|             // 参数说明:(在起始单元格的X坐标0-1023,Y的坐标0-255,在终止单元格的X坐标0-1023,Y的坐标0-255,起始单元格列数,行数,终止单元格列数,行数)  
 | ||
|             IClientAnchor anchor = patriarch.CreateAnchor(0, 0, 0, 0, columnIndex, rowIndex, columnIndex+1, rowIndex + 1);
 | ||
|             // 第五步:把图片插到相应的位置+1
 | ||
|             IPicture pict = patriarch.CreatePicture(anchor, pictureIdx); 
 | ||
| 
 | ||
|              
 | ||
|         }
 | ||
|         /// <summary>
 | ||
|         /// 设置单元格的值
 | ||
|         /// 注:只对当前ActiveSheet有效
 | ||
|         /// </summary>
 | ||
|         /// <param name="rowIndex">行号</param>
 | ||
|         /// <param name="columnIndex">列号</param>
 | ||
|         /// <param name="value">单元格的值</param>
 | ||
|         /// <param name="isFormula">是否是公式</param>
 | ||
|         public void SetValue(int rowIndex, int columnIndex, object value, bool isFormula)
 | ||
|         {
 | ||
|             IRow row = Book.GetSheetAt(sheetID).GetRow(rowIndex);
 | ||
|             if (row == null)
 | ||
|             {
 | ||
|                 row = Book.GetSheetAt(sheetID).CreateRow(rowIndex);
 | ||
|             }
 | ||
|             ICell cell = row.GetCell(columnIndex);
 | ||
|             if (cell == null)
 | ||
|             {
 | ||
|                 cell = row.CreateCell(columnIndex);
 | ||
|             }
 | ||
|             if (value == null)
 | ||
|             {
 | ||
|                 cell.SetCellValue("");
 | ||
|             }
 | ||
|             if (isFormula)
 | ||
|             {
 | ||
|                 cell.SetCellFormula(value.ToString());
 | ||
|             }
 | ||
|             else
 | ||
|             {
 | ||
|                 if (value is short)
 | ||
|                 {
 | ||
|                     cell.SetCellValue((short)value);
 | ||
|                 }
 | ||
|                 else if (value is int)
 | ||
|                 {
 | ||
|                     cell.SetCellValue((int)value);
 | ||
|                 }
 | ||
|                 else if (value is long)
 | ||
|                 {
 | ||
|                     cell.SetCellValue((long)value);
 | ||
|                 }
 | ||
|                 else if (value is float)
 | ||
|                 {
 | ||
|                     cell.SetCellValue((float)value);
 | ||
|                 }
 | ||
|                 else if (value is double)
 | ||
|                 {
 | ||
|                     cell.SetCellValue((double)value);
 | ||
|                 }
 | ||
|                 else if (value is bool)
 | ||
|                 {
 | ||
|                     cell.SetCellValue((bool)value);
 | ||
|                 }
 | ||
|                 else if (value is DateTime)
 | ||
|                 {
 | ||
|                     cell.SetCellValue((DateTime)value);
 | ||
|                 }
 | ||
|                 else if (value == null)
 | ||
|                 {
 | ||
|                 }
 | ||
|                 else
 | ||
|                 {
 | ||
|                     cell.SetCellValue(value.ToString());
 | ||
|                 }
 | ||
|             }
 | ||
| 
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 设置一个区域内的单元格的值范围
 | ||
|         /// 注:只对当前ActiveSheet有效
 | ||
|         /// </summary>
 | ||
|         /// <param name="startRowIndex">开始行</param>
 | ||
|         /// <param name="EndRowIndex">结束行</param>
 | ||
|         /// <param name="startColInex">开始列</param>
 | ||
|         /// <param name="endColIndex">结束列</param>
 | ||
|         /// <param name="type">验证类型</param>
 | ||
|         /// <param name="operatorType">验证方式</param>
 | ||
|         /// <param name="minValue">最小值</param>
 | ||
|         /// <param name="maxValue">最大值</param>
 | ||
|         public void SetValueRange(int startRowIndex, int EndRowIndex, int startColInex, int endColIndex, NPOIDataType type, OperatorTypes operatorType, String minValue, String maxValue)
 | ||
|         {
 | ||
|             SetValueRange(startRowIndex, EndRowIndex, startColInex, endColIndex, type, operatorType, minValue, maxValue, "", "");
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 设置一个区域内的单元格的值范围
 | ||
|         /// 注:只对当前ActiveSheet有效
 | ||
|         /// </summary>
 | ||
|         /// <param name="startRowIndex">开始行</param>
 | ||
|         /// <param name="EndRowIndex">结束行</param>
 | ||
|         /// <param name="startColInex">开始列</param>
 | ||
|         /// <param name="endColIndex">结束列</param>
 | ||
|         /// <param name="type">验证类型</param>
 | ||
|         /// <param name="operatorType">验证方式</param>
 | ||
|         /// <param name="minValue">最小值</param>
 | ||
|         /// <param name="maxValue">最大值</param>
 | ||
|         /// <param name="formate">数据格式</param>
 | ||
|         public void SetValueRange(int startRowIndex, int EndRowIndex, int startColInex, int endColIndex, NPOIDataType type, OperatorTypes operatorType, String minValue, String maxValue, String formate)
 | ||
|         {
 | ||
|             SetValueRange(startRowIndex, EndRowIndex, startColInex, endColIndex, type, operatorType, minValue, maxValue, formate, "");
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 设置一个区域内的单元格的值范围
 | ||
|         /// 注:只对当前ActiveSheet有效
 | ||
|         /// </summary>
 | ||
|         /// <param name="startRowIndex">开始行</param>
 | ||
|         /// <param name="EndRowIndex">结束行</param>
 | ||
|         /// <param name="startColInex">开始列</param>
 | ||
|         /// <param name="endColIndex">结束列</param>
 | ||
|         /// <param name="type">验证类型</param>
 | ||
|         /// <param name="operatorType">验证方式</param>
 | ||
|         /// <param name="minValue">最小值</param>
 | ||
|         /// <param name="maxValue">最大值</param>
 | ||
|         /// <param name="formate">数据格式</param>
 | ||
|         /// <param name="AlertMassage">报错信息</param>
 | ||
|         public void SetValueRange(int startRowIndex, int EndRowIndex, int startColInex, int endColIndex, NPOIDataType type, OperatorTypes operatorType, String minValue, String maxValue, String formate, String AlertMassage)
 | ||
|         {
 | ||
|             CellRangeAddressList regions = new CellRangeAddressList(startRowIndex, EndRowIndex, startColInex, endColIndex);
 | ||
|             DVConstraint constraint = DVConstraint.CreateNumericConstraint(ValidationType.ANY, 0, null, null);
 | ||
|             switch (type)
 | ||
|             {
 | ||
|                 case NPOIDataType.Integer:
 | ||
|                     constraint = DVConstraint.CreateNumericConstraint(ValidationType.INTEGER, (int)operatorType, minValue, maxValue);
 | ||
|                     break;
 | ||
|                 case NPOIDataType.Float:
 | ||
|                     constraint = DVConstraint.CreateNumericConstraint(ValidationType.DECIMAL, (int)operatorType, minValue, maxValue);
 | ||
|                     break;
 | ||
|                 case NPOIDataType.Date:
 | ||
|                     if (formate == "")
 | ||
|                     {
 | ||
|                         formate = "yyyy/MM/dd";
 | ||
|                     }
 | ||
|                     constraint = DVConstraint.CreateDateConstraint((int)operatorType, minValue, maxValue, formate);
 | ||
|                     break;
 | ||
|                 case NPOIDataType.Time:
 | ||
|                     constraint = DVConstraint.CreateTimeConstraint((int)operatorType, minValue, maxValue);
 | ||
|                     break;
 | ||
|                 case NPOIDataType.TextLength:
 | ||
|                     constraint = DVConstraint.CreateNumericConstraint(ValidationType.TEXT_LENGTH, (int)operatorType, minValue, maxValue);
 | ||
|                     break;
 | ||
|                 default:
 | ||
|                     break;
 | ||
|             }
 | ||
| 
 | ||
|             HSSFDataValidation dataValidate1 = new HSSFDataValidation(regions, constraint);
 | ||
|             if (!String.IsNullOrEmpty(AlertMassage))
 | ||
|             {
 | ||
|                 dataValidate1.CreateErrorBox("Error", AlertMassage);
 | ||
|             }
 | ||
|             ActiveSheet.AddValidationData(dataValidate1);
 | ||
| 
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 设置一个区域内的单元格的值范围
 | ||
|         /// 注:只对当前ActiveSheet有效
 | ||
|         /// </summary>
 | ||
|         /// <param name="startRowIndex">开始行</param>
 | ||
|         /// <param name="EndRowIndex">结束行</param>
 | ||
|         /// <param name="startColInex">开始列</param>
 | ||
|         /// <param name="endColIndex">结束列</param>
 | ||
|         /// <param name="dataRange">值系列</param>
 | ||
|         public void SetValueRange(int startRowIndex, int EndRowIndex, int startColInex, int endColIndex, String[] dataRange)
 | ||
|         {
 | ||
| 
 | ||
|             SetValueRange(startRowIndex, EndRowIndex, startColInex, endColIndex, dataRange, "");
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 设置一个区域内的单元格的值范围
 | ||
|         /// 注:只对当前ActiveSheet有效
 | ||
|         /// </summary>
 | ||
|         /// <param name="startRowIndex">开始行</param>
 | ||
|         /// <param name="EndRowIndex">结束行</param>
 | ||
|         /// <param name="startColInex">开始列</param>
 | ||
|         /// <param name="endColIndex">结束列</param>
 | ||
|         /// <param name="dataRange">值系列</param>
 | ||
|         /// <param name="alertMassage">报错信息</param>
 | ||
|         public void SetValueRange(int startRowIndex, int endRowIndex, int startColInex, int endColIndex, String[] dataRange, String alertMassage)
 | ||
|         {
 | ||
|             ISheetConditionalFormatting hscf = ActiveSheet.SheetConditionalFormatting;
 | ||
|             CellRangeAddress[] regions = {
 | ||
|                 new CellRangeAddress(startRowIndex, endRowIndex,startColInex,endColIndex)
 | ||
|             };
 | ||
| 
 | ||
|             CellRangeAddressList rangeList = new CellRangeAddressList();
 | ||
|             rangeList.AddCellRangeAddress(new CellRangeAddress(startRowIndex, endRowIndex, startColInex, endColIndex));
 | ||
|             DVConstraint dvconstraint = DVConstraint.CreateExplicitListConstraint(dataRange);
 | ||
|             HSSFDataValidation dataValidation = new HSSFDataValidation(rangeList, dvconstraint);
 | ||
| 
 | ||
|             if (!String.IsNullOrEmpty(alertMassage))
 | ||
|             {
 | ||
|                 dataValidation.CreateErrorBox("Error", alertMassage);
 | ||
|             }
 | ||
| 
 | ||
|             ActiveSheet.AddValidationData(dataValidation);
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 设置一个区域内的单元格的值范围
 | ||
|         /// 注:只对当前ActiveSheet有效
 | ||
|         /// </summary>
 | ||
|         /// <param name="startRowIndex">开始行</param>
 | ||
|         /// <param name="EndRowIndex">结束行</param>
 | ||
|         /// <param name="startColInex">开始列</param>
 | ||
|         /// <param name="endColIndex">结束列</param>
 | ||
|         /// <param name="Formula">计算公式</param>
 | ||
|         /// <param name="alertMassage">报错信息</param>
 | ||
|         public void SetValueRange(int startRowIndex, int endRowIndex, int startColInex, int endColIndex, String Formula, String alertMassage)
 | ||
|         {
 | ||
|             ISheetConditionalFormatting hscf = ActiveSheet.SheetConditionalFormatting;
 | ||
|             CellRangeAddress[] regions = {
 | ||
|                 new CellRangeAddress(startRowIndex, endRowIndex,startColInex,endColIndex)
 | ||
|             };
 | ||
| 
 | ||
|             CellRangeAddressList rangeList = new CellRangeAddressList();
 | ||
|             rangeList.AddCellRangeAddress(new CellRangeAddress(startRowIndex, endRowIndex, startColInex, endColIndex));
 | ||
|             DVConstraint dvconstraint = DVConstraint.CreateFormulaListConstraint(Formula);
 | ||
|             HSSFDataValidation dataValidation = new HSSFDataValidation(rangeList, dvconstraint);
 | ||
| 
 | ||
|             if (!String.IsNullOrEmpty(alertMassage))
 | ||
|             {
 | ||
|                 dataValidation.CreateErrorBox("Error", alertMassage);
 | ||
|             }
 | ||
| 
 | ||
|             ActiveSheet.AddValidationData(dataValidation);
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 设置一个区域内的单元格的值范围
 | ||
|         /// 注:只对当前ActiveSheet有效
 | ||
|         /// </summary>
 | ||
|         /// <param name="startRowIndex">开始行</param>
 | ||
|         /// <param name="EndRowIndex">结束行</param>
 | ||
|         /// <param name="startColInex">开始列</param>
 | ||
|         /// <param name="endColIndex">结束列</param>
 | ||
|         /// <param name="Formula">计算公式</param>
 | ||
|         public void SetValueRange(int startRowIndex, int endRowIndex, int startColInex, int endColIndex, String Formula)
 | ||
|         {
 | ||
|             SetValueRange(startRowIndex, endColIndex, startRowIndex, endColIndex, Formula, "");
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 生成单元格样式
 | ||
|         /// </summary>
 | ||
|         /// <returns>与当前Excel相关的单元格样式</returns>
 | ||
|         public ICellStyle CreateCellStyle()
 | ||
|         {
 | ||
|             return Book.CreateCellStyle();
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 生成字体
 | ||
|         /// </summary>
 | ||
|         /// <returns>与当前Excel相关的字体</returns>
 | ||
|         public IFont CreateFont()
 | ||
|         {
 | ||
|             return Book.CreateFont();
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 设置单元格样式
 | ||
|         /// 注:只对当前ActiveSheet有效
 | ||
|         /// </summary>
 | ||
|         /// <param name="rowIndex">行号</param>
 | ||
|         /// <param name="columnIndex">列号</param>
 | ||
|         /// <param name="style">样式</param>
 | ||
|         public void SetStyle(int rowIndex, int columnIndex, ICellStyle style)
 | ||
|         {
 | ||
|             IRow row = Book.GetSheetAt(sheetID).GetRow(rowIndex);
 | ||
|             if (row == null)
 | ||
|             {
 | ||
|                 row = Book.GetSheetAt(sheetID).CreateRow(rowIndex);
 | ||
|             }
 | ||
|             ICell cell = row.GetCell(columnIndex);
 | ||
|             if (cell == null)
 | ||
|             {
 | ||
|                 cell = row.CreateCell(columnIndex);
 | ||
|             }
 | ||
|             cell.CellStyle = style;
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 合并单元格
 | ||
|         /// 注:只对当前ActiveSheet有效
 | ||
|         /// </summary>
 | ||
|         /// <param name="startRowIndex">开始行号</param>
 | ||
|         /// <param name="startColumnIndex">开始列号</param>
 | ||
|         /// <param name="endRowIndex">结束行号</param>
 | ||
|         /// <param name="endColumnIndex">结束列号</param>
 | ||
|         public void MergeCells(int startRowIndex, int startColumnIndex, int endRowIndex, int endColumnIndex)
 | ||
|         {
 | ||
|             int Index = Book.GetSheetAt(sheetID).AddMergedRegion(new CellRangeAddress(startRowIndex, endRowIndex, startColumnIndex, endColumnIndex));
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 拆分单元格
 | ||
|         /// 注1:只对当前ActiveSheet有效
 | ||
|         /// 注2:只有合并的单元格才能拆分
 | ||
|         /// </summary>
 | ||
|         /// <param name="startRowIndex">开始行号</param>
 | ||
|         /// <param name="startColumnIndex">开始列号</param>
 | ||
|         public void UnMergeCells(int startRowIndex, int startColumnIndex)
 | ||
|         {
 | ||
|             int merges = Book.GetSheetAt(sheetID).NumMergedRegions;
 | ||
|             CellRangeAddress merge;
 | ||
|             for (int i = 0; i < merges; i++)
 | ||
|             {
 | ||
|                 merge = Book.GetSheetAt(sheetID).GetMergedRegion(i);
 | ||
|                 if (merge.FirstRow == startRowIndex && merge.FirstColumn == startColumnIndex)
 | ||
|                 {
 | ||
|                     Book.GetSheetAt(sheetID).RemoveMergedRegion(i);
 | ||
|                     break;
 | ||
|                 }
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 保存到文件
 | ||
|         /// 注:有模板的,文件扩展名与模板一样;没有模板的,文件扩展名为“.xls”;
 | ||
|         /// </summary>
 | ||
|         /// <param name="fileName">保存文件名</param>
 | ||
|         public void Save(String fileName)
 | ||
|         {
 | ||
|             FileStream file = new FileStream(fileName, FileMode.Create);
 | ||
|             Book.Write(file);
 | ||
|             file.Close();
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 保存到流
 | ||
|         /// 注:保存或下载时,有模板的,文件扩展名与模板一样;没有模板的,文件扩展名为“.xls”;
 | ||
|         /// </summary>
 | ||
|         /// <returns>内存流</returns>
 | ||
|         public MemoryStream Save()
 | ||
|         {
 | ||
|             MemoryStream ms = new MemoryStream();
 | ||
|             Book.Write(ms);
 | ||
|             return ms;
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 把Excel读成DataSet
 | ||
|         /// 注:必须是正规表格式
 | ||
|         /// </summary>
 | ||
|         /// <returns>读出的Excel</returns>
 | ||
|         public DataSet ReadAsDataSet()
 | ||
|         {
 | ||
|             DataSet rtn = new DataSet();
 | ||
|             for (int i = 0; i < SheetCount; i++)
 | ||
|             {
 | ||
|                 ISheet sheet = Book.GetSheetAt(i);
 | ||
|                 rtn.Tables.Add(GetDataTableBySheet(sheet));
 | ||
|             }
 | ||
|             return rtn;
 | ||
|         }
 | ||
| 
 | ||
|         private DataTable GetDataTableBySheet(ISheet sheet)
 | ||
|         {
 | ||
|             DataTable dt = new DataTable(sheet.SheetName);
 | ||
|             int maxCols = 0;
 | ||
|             object value;
 | ||
|             while ((value = ReadValue(sheet, 0, maxCols)) != null)
 | ||
|             {
 | ||
|                 dt.Columns.Add(value.ToString());
 | ||
|                 maxCols++;
 | ||
|             }
 | ||
| 
 | ||
|             int row = 1;
 | ||
| 
 | ||
|             bool emptyRow = false;
 | ||
|             int emptyRowCount = 0;
 | ||
|             while (emptyRowCount < 10)
 | ||
|             {
 | ||
|                 emptyRow = true;
 | ||
|                 DataRow dr = dt.NewRow();
 | ||
|                 for (int i = 0; i < maxCols; i++)
 | ||
|                 {
 | ||
|                     value = ReadValue(sheet, row, i);
 | ||
|                     if (value != null)
 | ||
|                     {
 | ||
|                         dr[i] = value;
 | ||
|                         emptyRow = false;
 | ||
|                     }
 | ||
|                 }
 | ||
|                 if (!emptyRow)
 | ||
|                 {
 | ||
|                     dt.Rows.Add(dr);
 | ||
|                     emptyRowCount = 0;
 | ||
|                 }
 | ||
|                 else
 | ||
|                 {
 | ||
|                     emptyRowCount++;
 | ||
|                 }
 | ||
|                 row++;
 | ||
|             }
 | ||
| 
 | ||
|             return dt;
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 根据SheetName导出数据为DataTable
 | ||
|         /// </summary>
 | ||
|         /// <param name="sheetName">Sheet名称</param>
 | ||
|         /// <returns></returns>
 | ||
|         public DataTable GetDataTableBySheet(String sheetName)
 | ||
|         {
 | ||
| 
 | ||
|             ISheet sheet = Book.GetSheet(sheetName);
 | ||
|             if (sheet != null)
 | ||
|             {
 | ||
|                 return GetDataTableBySheet(sheet);
 | ||
|             }
 | ||
|             return null;
 | ||
| 
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 根据SheetName导出数据为DataTable
 | ||
|         /// </summary>
 | ||
|         /// <param name="sheetIndex">Sheet编号</param>
 | ||
|         /// <returns></returns>
 | ||
|         public DataTable GetDataTableBySheet(int sheetIndex)
 | ||
|         {
 | ||
| 
 | ||
|             ISheet sheet = Book.GetSheetAt(sheetIndex);
 | ||
|             if (sheet != null)
 | ||
|             {
 | ||
|                 return GetDataTableBySheet(sheet);
 | ||
|             }
 | ||
|             return null;
 | ||
| 
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 写入表格
 | ||
|         /// </summary>
 | ||
|         /// <param name="Data">表格数据</param>
 | ||
|         /// <param name="col">写入的起始列</param>
 | ||
|         /// <param name="row">写入的起始行</param>
 | ||
|         /// <param name="titleColor">标题颜色</param>
 | ||
|         /// <param name="fullBorder">是否需要四周边框</param>
 | ||
|         public void WriteDataTable(DataTable Data, int col = 1, int row = 1, short? titleColor = null, bool fullBorder = true)
 | ||
|         {
 | ||
|             if (Data == null)
 | ||
|             {
 | ||
|                 return;
 | ||
|             }
 | ||
| 
 | ||
|             var titleStyle = CreateCellStyle();
 | ||
|             var rowStyle = CreateCellStyle();
 | ||
|             if (titleColor != null)
 | ||
|                 titleStyle.FillForegroundColor = titleColor.Value;
 | ||
| 
 | ||
|             titleStyle.FillPattern = FillPattern.SolidForeground;
 | ||
|             if (fullBorder)
 | ||
|             {
 | ||
|                 titleStyle.BorderBottom = BorderStyle.Thin;
 | ||
|                 titleStyle.BorderLeft = BorderStyle.Thin;
 | ||
|                 titleStyle.BorderRight = BorderStyle.Thin;
 | ||
|                 titleStyle.BorderTop = BorderStyle.Thin;
 | ||
|                 titleStyle.BottomBorderColor = NPOIColor.BLACK;
 | ||
|                 titleStyle.LeftBorderColor = NPOIColor.BLACK;
 | ||
|                 titleStyle.RightBorderColor = NPOIColor.BLACK;
 | ||
|                 titleStyle.TopBorderColor = NPOIColor.BLACK;
 | ||
|                 rowStyle.BorderBottom = BorderStyle.Thin;
 | ||
|                 rowStyle.BorderLeft = BorderStyle.Thin;
 | ||
|                 rowStyle.BorderRight = BorderStyle.Thin;
 | ||
|                 rowStyle.BorderTop = BorderStyle.Thin;
 | ||
|                 rowStyle.BottomBorderColor = NPOIColor.BLACK;
 | ||
|                 rowStyle.LeftBorderColor = NPOIColor.BLACK;
 | ||
|                 rowStyle.RightBorderColor = NPOIColor.BLACK;
 | ||
|                 rowStyle.TopBorderColor = NPOIColor.BLACK;
 | ||
|             }
 | ||
| 
 | ||
|             int iCol = 0, iRow = 1;
 | ||
| 
 | ||
|             foreach (DataColumn dc in Data.Columns)
 | ||
|             {
 | ||
|                 SetValue(row, col + iCol, dc.ColumnName);
 | ||
|                 SetStyle(row, col + iCol, titleStyle);
 | ||
|                 iCol++;
 | ||
|             }
 | ||
| 
 | ||
|             rowStyle.FillForegroundColor = NPOIColor.WHITE;
 | ||
| 
 | ||
|             foreach (DataRow dr in Data.Rows)
 | ||
|             {
 | ||
|                 iCol = 0;
 | ||
|                 foreach (DataColumn dc in Data.Columns)
 | ||
|                 {
 | ||
|                     SetValue(row + iRow, col + iCol, dr[dc]);
 | ||
|                     SetStyle(row + iRow, col + iCol, rowStyle);
 | ||
|                     iCol++;
 | ||
|                 }
 | ||
|                 iRow++;
 | ||
|             }
 | ||
| 
 | ||
|             for (int i = 0; i < iCol; i++)
 | ||
|             {
 | ||
|                 this.AutoColWidth(i);
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 读取单元格的值
 | ||
|         /// 注:只对当前ActiveSheet有效
 | ||
|         /// </summary>
 | ||
|         /// <param name="rowIndex">行号</param>
 | ||
|         /// <param name="columnIndex">列号</param>
 | ||
|         /// <returns>单元格的值</returns>
 | ||
|         public object ReadValue(ISheet sheet, int rowIndex, int columnIndex, bool? isDateTime = null)
 | ||
|         {
 | ||
|             try
 | ||
|             {
 | ||
|                 ICell cell = sheet.GetRow(rowIndex).GetCell(columnIndex);
 | ||
|                 short df = cell.CellStyle.DataFormat;
 | ||
| 
 | ||
|                 //return cell.ToString();
 | ||
|                 switch (cell.CellType)
 | ||
|                 {
 | ||
|                     case CellType.Blank:
 | ||
|                         return null;
 | ||
|                     case CellType.Boolean:
 | ||
|                         return cell.BooleanCellValue;
 | ||
|                     case CellType.Error:
 | ||
|                         throw new Exception("Cell Value Error");
 | ||
|                     case CellType.Formula:
 | ||
|                         {
 | ||
|                             switch (cell.CachedFormulaResultType)
 | ||
|                             {
 | ||
|                                 case CellType.Blank:
 | ||
|                                     return "";
 | ||
|                                 case CellType.Boolean:
 | ||
|                                     return cell.BooleanCellValue;
 | ||
|                                 case CellType.Error:
 | ||
|                                     throw new Exception("Cell Value Error");
 | ||
|                                 case CellType.Formula:
 | ||
|                                     throw new Exception("The Formula of this cell is too complex!");
 | ||
|                                 case CellType.Numeric:
 | ||
|                                     if (isDateTime == null)
 | ||
|                                     {
 | ||
|                                         if (DateUtil.IsCellDateFormatted(cell))
 | ||
|                                         {
 | ||
|                                             return cell.DateCellValue;
 | ||
|                                         }
 | ||
|                                         else
 | ||
|                                         {
 | ||
|                                             return cell.NumericCellValue;
 | ||
|                                         }
 | ||
|                                     }
 | ||
|                                     else if (isDateTime == true)
 | ||
|                                     {
 | ||
|                                         return cell.DateCellValue;
 | ||
|                                     }
 | ||
|                                     else
 | ||
|                                     {
 | ||
|                                         return cell.NumericCellValue;
 | ||
|                                     }
 | ||
|                                 case CellType.String:
 | ||
|                                     return cell.StringCellValue;
 | ||
|                                 case CellType.Unknown:
 | ||
|                                     return cell.ToString();
 | ||
|                                 default:
 | ||
|                                     return cell.ToString();
 | ||
|                             }
 | ||
|                         }
 | ||
|                     case CellType.Numeric:
 | ||
|                         {
 | ||
|                             if (isDateTime == null)
 | ||
|                             {
 | ||
|                                 if (DateUtil.IsCellDateFormatted(cell))
 | ||
|                                 {
 | ||
|                                     return cell.DateCellValue;
 | ||
|                                 }
 | ||
|                                 else
 | ||
|                                 {
 | ||
|                                     return cell.NumericCellValue;
 | ||
|                                 }
 | ||
|                             }
 | ||
|                             else if (isDateTime == true)
 | ||
|                             {
 | ||
|                                 return cell.DateCellValue;
 | ||
|                             }
 | ||
|                             else
 | ||
|                             {
 | ||
|                                 return cell.NumericCellValue;
 | ||
|                             }
 | ||
|                         }
 | ||
|                     case CellType.String:
 | ||
|                         return cell.StringCellValue;
 | ||
|                     case CellType.Unknown:
 | ||
|                         return cell.ToString();
 | ||
|                     default:
 | ||
|                         return cell.ToString();
 | ||
|                 }
 | ||
|             }
 | ||
|             catch (System.NullReferenceException)
 | ||
|             {
 | ||
|                 return null;
 | ||
|             }
 | ||
|             catch (Exception ex)
 | ||
|             {
 | ||
|                 throw ex;
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         public int SheetCount
 | ||
|         {
 | ||
|             get
 | ||
|             {
 | ||
|                 return Book.NumberOfSheets;
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         public String GetSheetName(int Index)
 | ||
|         {
 | ||
|             return Book.GetSheetName(Index);
 | ||
|         }
 | ||
| 
 | ||
|         public void AddPicture(byte[] data, int row, int col)
 | ||
|         {
 | ||
|             int picIndex = Book.AddPicture(data, PictureType.PNG);
 | ||
| 
 | ||
|             IDrawing draw = ActiveSheet.CreateDrawingPatriarch();
 | ||
| 
 | ||
|             IClientAnchor anchor = draw.CreateAnchor(0, 0, 255, 255, col, row, col + 5, col + 5);
 | ||
|             IPicture pic = draw.CreatePicture(anchor, picIndex);
 | ||
|             pic.Resize();
 | ||
|         }
 | ||
|     }
 | ||
| 
 | ||
|     public enum OperatorTypes
 | ||
|     {
 | ||
|         /// <summary>
 | ||
|         /// 介于最大值与小值之间
 | ||
|         /// </summary>
 | ||
|         BETWEEN = OperatorType.BETWEEN,
 | ||
|         /// <summary>
 | ||
|         /// 等于最小值
 | ||
|         /// </summary>
 | ||
|         EQUAL = OperatorType.EQUAL,
 | ||
|         /// <summary>
 | ||
|         /// 大于或等于最小值
 | ||
|         /// </summary>
 | ||
|         GREATER_OR_EQUAL = OperatorType.GREATER_OR_EQUAL,
 | ||
|         /// <summary>
 | ||
|         /// 大于最小值
 | ||
|         /// </summary>
 | ||
|         GREATER_THAN = OperatorType.GREATER_THAN,
 | ||
|         /// <summary>
 | ||
|         /// 忽略
 | ||
|         /// </summary>
 | ||
|         NO_COMPARISON = OperatorType.IGNORED,
 | ||
|         /// <summary>
 | ||
|         /// 小于或等于最小值
 | ||
|         /// </summary>
 | ||
|         LESS_OR_EQUAL = OperatorType.LESS_OR_EQUAL,
 | ||
|         /// <summary>
 | ||
|         /// 小于最小值
 | ||
|         /// </summary>
 | ||
|         LESS_THAN = OperatorType.LESS_THAN,
 | ||
|         /// <summary>
 | ||
|         /// 不在最小值与最大值之间
 | ||
|         /// </summary>
 | ||
|         NOT_BETWEEN = OperatorType.NOT_BETWEEN,
 | ||
|         /// <summary>
 | ||
|         /// 不等于最小值
 | ||
|         /// </summary>
 | ||
|         NOT_EQUAL = OperatorType.NOT_EQUAL
 | ||
|     }
 | ||
| 
 | ||
|     public enum NPOIDataType
 | ||
|     {
 | ||
|         /// <summary>
 | ||
|         /// 验证整数
 | ||
|         /// </summary>
 | ||
|         Integer,
 | ||
|         /// <summary>
 | ||
|         /// 验证符点数
 | ||
|         /// </summary>
 | ||
|         Float,
 | ||
|         /// <summary>
 | ||
|         /// 验证日期
 | ||
|         /// </summary>
 | ||
|         Date,
 | ||
|         /// <summary>
 | ||
|         /// 验证时间
 | ||
|         /// </summary>
 | ||
|         Time,
 | ||
|         /// <summary>
 | ||
|         /// 验证字符长度
 | ||
|         /// </summary>
 | ||
|         TextLength
 | ||
|     }
 | ||
| 
 | ||
|     public static class NPOIColor
 | ||
|     {
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 红色
 | ||
|         /// </summary>
 | ||
|         public static short RED { get { return NPOI.HSSF.Util.HSSFColor.Red.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 蓝色
 | ||
|         /// </summary>
 | ||
|         public static short BLUE { get { return NPOI.HSSF.Util.HSSFColor.Blue.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 浅绿色
 | ||
|         /// </summary>
 | ||
|         public static short AQUA { get { return NPOI.HSSF.Util.HSSFColor.Aqua.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 自动
 | ||
|         /// </summary>
 | ||
|         public static short AUTOMATIC { get { return NPOI.HSSF.Util.HSSFColor.Automatic.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 黑色
 | ||
|         /// </summary>
 | ||
|         public static short BLACK { get { return NPOI.HSSF.Util.HSSFColor.Black.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 蓝灰色
 | ||
|         /// </summary>
 | ||
|         public static short BLUE_GREY { get { return NPOI.HSSF.Util.HSSFColor.BlueGrey.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 明绿色
 | ||
|         /// </summary>
 | ||
|         public static short BRIGHT_GREEN { get { return NPOI.HSSF.Util.HSSFColor.BrightGreen.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 棕色
 | ||
|         /// </summary>
 | ||
|         public static short BROWN { get { return NPOI.HSSF.Util.HSSFColor.Brown.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 正常
 | ||
|         /// </summary>
 | ||
|         public static short COLOR_NORMAL { get { return NPOI.HSSF.Util.HSSFColor.COLOR_NORMAL; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 珊瑚色
 | ||
|         /// </summary>
 | ||
|         public static short CORAL { get { return NPOI.HSSF.Util.HSSFColor.Coral.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 亮蓝色
 | ||
|         /// </summary>
 | ||
|         public static short CORNFLOWER_BLUE { get { return NPOI.HSSF.Util.HSSFColor.CornflowerBlue.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 深蓝色
 | ||
|         /// </summary>
 | ||
|         public static short DARK_BLUE { get { return NPOI.HSSF.Util.HSSFColor.DarkBlue.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 深绿色
 | ||
|         /// </summary>
 | ||
|         public static short DARK_GREEN { get { return NPOI.HSSF.Util.HSSFColor.DarkGreen.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 深红色
 | ||
|         /// </summary>
 | ||
|         public static short DARK_RED { get { return NPOI.HSSF.Util.HSSFColor.DarkRed.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 深茶色
 | ||
|         /// </summary>
 | ||
|         public static short DARK_TEAL { get { return NPOI.HSSF.Util.HSSFColor.DarkTeal.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 深黄
 | ||
|         /// </summary>
 | ||
|         public static short DARK_YELLOW { get { return NPOI.HSSF.Util.HSSFColor.DarkYellow.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 金色
 | ||
|         /// </summary>
 | ||
|         public static short GOLD { get { return NPOI.HSSF.Util.HSSFColor.Gold.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 绿色
 | ||
|         /// </summary>
 | ||
|         public static short GREEN { get { return NPOI.HSSF.Util.HSSFColor.Green.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 25%灰色
 | ||
|         /// </summary>
 | ||
|         public static short GREY_25_PERCENT { get { return NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 40%灰色
 | ||
|         /// </summary>
 | ||
|         public static short GREY_40_PERCENT { get { return NPOI.HSSF.Util.HSSFColor.Grey40Percent.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 50%灰色
 | ||
|         /// </summary>
 | ||
|         public static short GREY_50_PERCENT { get { return NPOI.HSSF.Util.HSSFColor.Grey50Percent.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 80%灰色
 | ||
|         /// </summary>
 | ||
|         public static short GREY_80_PERCENT { get { return NPOI.HSSF.Util.HSSFColor.Grey80Percent.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 靛蓝色
 | ||
|         /// </summary>
 | ||
|         public static short INDIGO { get { return NPOI.HSSF.Util.HSSFColor.Indigo.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 淡紫色
 | ||
|         /// </summary>
 | ||
|         public static short LAVENDER { get { return NPOI.HSSF.Util.HSSFColor.Lavender.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 粉黄色
 | ||
|         /// </summary>
 | ||
|         public static short LEMON_CHIFFON { get { return NPOI.HSSF.Util.HSSFColor.LemonChiffon.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 淡蓝色
 | ||
|         /// </summary>
 | ||
|         public static short LIGHT_BLUE { get { return NPOI.HSSF.Util.HSSFColor.LightBlue.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 淡亮蓝色
 | ||
|         /// </summary>
 | ||
|         public static short LIGHT_CORNFLOWER_BLUE { get { return NPOI.HSSF.Util.HSSFColor.LightCornflowerBlue.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 淡绿色
 | ||
|         /// </summary>
 | ||
|         public static short LIGHT_GREEN { get { return NPOI.HSSF.Util.HSSFColor.LightGreen.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 淡桔黄色
 | ||
|         /// </summary>
 | ||
|         public static short LIGHT_ORANGE { get { return NPOI.HSSF.Util.HSSFColor.LightOrange.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 淡蓝绿色
 | ||
|         /// </summary>
 | ||
|         public static short LIGHT_TURQUOISE { get { return NPOI.HSSF.Util.HSSFColor.LightTurquoise.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 淡黄色
 | ||
|         /// </summary>
 | ||
|         public static short LIGHT_YELLOW { get { return NPOI.HSSF.Util.HSSFColor.LightYellow.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 绿黄色
 | ||
|         /// </summary>
 | ||
|         public static short LIME { get { return NPOI.HSSF.Util.HSSFColor.Lime.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 栗色
 | ||
|         /// </summary>
 | ||
|         public static short MAROON { get { return NPOI.HSSF.Util.HSSFColor.Maroon.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 橄榄绿色
 | ||
|         /// </summary>
 | ||
|         public static short OLIVE_GREEN { get { return NPOI.HSSF.Util.HSSFColor.OliveGreen.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 桔色
 | ||
|         /// </summary>
 | ||
|         public static short ORANGE { get { return NPOI.HSSF.Util.HSSFColor.Orange.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 白灰蓝色
 | ||
|         /// </summary>
 | ||
|         public static short PALE_BLUE { get { return NPOI.HSSF.Util.HSSFColor.PaleBlue.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 粉红色
 | ||
|         /// </summary>
 | ||
|         public static short PINK { get { return NPOI.HSSF.Util.HSSFColor.Pink.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 紫红色
 | ||
|         /// </summary>
 | ||
|         public static short PLUM { get { return NPOI.HSSF.Util.HSSFColor.Plum.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 玫瑰红色
 | ||
|         /// </summary>
 | ||
|         public static short ROSE { get { return NPOI.HSSF.Util.HSSFColor.Rose.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 高贵蓝
 | ||
|         /// </summary>
 | ||
|         public static short ROYAL_BLUE { get { return NPOI.HSSF.Util.HSSFColor.RoyalBlue.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 海绿色
 | ||
|         /// </summary>
 | ||
|         public static short SEA_GREEN { get { return NPOI.HSSF.Util.HSSFColor.SeaGreen.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 天空蓝
 | ||
|         /// </summary>
 | ||
|         public static short SKY_BLUE { get { return NPOI.HSSF.Util.HSSFColor.SkyBlue.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 棕褐色
 | ||
|         /// </summary>
 | ||
|         public static short TAN { get { return NPOI.HSSF.Util.HSSFColor.Tan.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 茶色
 | ||
|         /// </summary>
 | ||
|         public static short TEAL { get { return NPOI.HSSF.Util.HSSFColor.Teal.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 蓝绿色
 | ||
|         /// </summary>
 | ||
|         public static short TURQUOISE { get { return NPOI.HSSF.Util.HSSFColor.Turquoise.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 紫色
 | ||
|         /// </summary>
 | ||
|         public static short VIOLET { get { return NPOI.HSSF.Util.HSSFColor.Violet.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 白色
 | ||
|         /// </summary>
 | ||
|         public static short WHITE { get { return NPOI.HSSF.Util.HSSFColor.White.Index; } }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 黄色
 | ||
|         /// </summary>
 | ||
|         public static short YELLOW { get { return NPOI.HSSF.Util.HSSFColor.Yellow.Index; } }
 | ||
| 
 | ||
|     }
 | ||
| 
 | ||
|     /// <summary>
 | ||
|     /// 针对excel的Oledb
 | ||
|     /// </summary>
 | ||
|     public class OleDbExcel
 | ||
|     {
 | ||
|         /// <summary>
 | ||
|         /// OLEDB连接
 | ||
|         /// </summary>
 | ||
|         public OleDbConnection Connection
 | ||
|         {
 | ||
|             get;
 | ||
|             set;
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 用Oledb对Excel进行操作
 | ||
|         /// 注:必须是标准表形式Excel内容
 | ||
|         /// </summary>
 | ||
|         /// <param name="excelFile">Excel文件</param>
 | ||
|         public OleDbExcel(String excelFile)
 | ||
|         {
 | ||
|             String conStr = String.Empty;
 | ||
|             FileInfo file = new FileInfo(excelFile);
 | ||
|             if (!file.Exists) { throw new Exception("文件不存在"); }
 | ||
|             String extension = file.Extension;
 | ||
|             switch (extension)
 | ||
|             {
 | ||
|                 case ".xls":
 | ||
|                     conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFile + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
 | ||
|                     break;
 | ||
|                 case ".xlsx":
 | ||
|                     conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFile + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;'";
 | ||
|                     break;
 | ||
|                 default:
 | ||
|                     conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFile + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
 | ||
|                     break;
 | ||
|             }
 | ||
| 
 | ||
|             //链接Excel
 | ||
|             Connection = new OleDbConnection(conStr);
 | ||
|         }
 | ||
| 
 | ||
|         private List<String> tableNames;
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 获取Excel内的Sheet名称
 | ||
|         /// </summary>
 | ||
|         public List<String> Sheets
 | ||
|         {
 | ||
|             get
 | ||
|             {
 | ||
|                 if (tableNames == null)
 | ||
|                 {
 | ||
|                     try
 | ||
|                     {
 | ||
|                         tableNames = new List<String>();
 | ||
|                         //读取Excel里面的sheet名
 | ||
|                         Connection.Open();
 | ||
| 
 | ||
|                         DataTable schemaTable = Connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
 | ||
| 
 | ||
|                         for (int i = 0; i < schemaTable.Rows.Count; i++)
 | ||
|                         {
 | ||
|                             DataRow dr = schemaTable.Rows[i];
 | ||
|                             String tbName = dr["table_name"].ToString();
 | ||
|                             if (tbName[tbName.Length - 1] == '$')
 | ||
|                             {
 | ||
|                                 tableNames.Add(tbName);
 | ||
|                             }
 | ||
|                         }
 | ||
|                         Connection.Close();
 | ||
|                     }
 | ||
|                     catch (Exception ex)
 | ||
|                     {
 | ||
|                         if (Connection.State != ConnectionState.Closed)
 | ||
|                         {
 | ||
|                             Connection.Close();
 | ||
|                         }
 | ||
|                         throw new Exception(ex.Message, ex);
 | ||
|                     }
 | ||
|                 }
 | ||
|                 return tableNames;
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 查询出所有数据
 | ||
|         /// </summary>
 | ||
|         /// <param name="tableName">Sheet名称</param>
 | ||
|         /// <returns>sheet内的所有数据</returns>
 | ||
|         public DataSet QueryAll(String tableName)
 | ||
|         {
 | ||
|             try
 | ||
|             {
 | ||
|                 DataSet excelData = new DataSet();
 | ||
|                 OleDbDataAdapter adapter = new OleDbDataAdapter();
 | ||
|                 adapter.SelectCommand = new OleDbCommand();
 | ||
|                 adapter.SelectCommand.Connection = Connection;
 | ||
|                 adapter.SelectCommand.CommandText = String.Format("SELECT * FROM {0}", "[" + tableName + "]");
 | ||
|                 adapter.Fill(excelData);
 | ||
|                 return excelData;
 | ||
|             }
 | ||
|             catch (Exception ex)
 | ||
|             {
 | ||
|                 if (Connection.State != ConnectionState.Closed)
 | ||
|                 {
 | ||
|                     Connection.Close();
 | ||
|                 }
 | ||
|                 throw new Exception(ex.Message, ex);
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 查询出所有数据
 | ||
|         /// </summary>
 | ||
|         /// <param name="tableIndex">Sheet序号(从0开始)</param>
 | ||
|         /// <returns>sheet内的所有数据</returns>
 | ||
|         public DataSet QueryAll(int tableIndex)
 | ||
|         {
 | ||
|             return QueryAll(Sheets[tableIndex]);
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 利用Sql进行查询
 | ||
|         /// </summary>
 | ||
|         /// <param name="sql">Sql语句</param>
 | ||
|         /// <returns>查询出的数据</returns>
 | ||
|         public DataSet Query(String sql)
 | ||
|         {
 | ||
|             try
 | ||
|             {
 | ||
|                 DataSet excelData = new DataSet();
 | ||
|                 OleDbDataAdapter adapter = new OleDbDataAdapter(sql, Connection);
 | ||
|                 adapter.Fill(excelData);
 | ||
|                 return excelData;
 | ||
|             }
 | ||
|             catch (Exception ex)
 | ||
|             {
 | ||
|                 if (Connection.State != ConnectionState.Closed)
 | ||
|                 {
 | ||
|                     Connection.Close();
 | ||
|                 }
 | ||
|                 throw new Exception(ex.Message, ex);
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 利用Sql进行查询
 | ||
|         /// </summary>
 | ||
|         /// <param name="sql">Sql语句</param>
 | ||
|         /// <param name="param">查询参数</param>
 | ||
|         /// <returns>查询出的数据</returns>
 | ||
|         public DataSet Query(String sql, params OleDbParameter[] param)
 | ||
|         {
 | ||
|             try
 | ||
|             {
 | ||
|                 DataSet excelData = new DataSet();
 | ||
|                 OleDbDataAdapter adapter = new OleDbDataAdapter(sql, Connection);
 | ||
|                 adapter.SelectCommand.Parameters.AddRange(param);
 | ||
|                 adapter.Fill(excelData);
 | ||
|                 return excelData;
 | ||
|             }
 | ||
|             catch (Exception ex)
 | ||
|             {
 | ||
|                 if (Connection.State != ConnectionState.Closed)
 | ||
|                 {
 | ||
|                     Connection.Close();
 | ||
|                 }
 | ||
|                 throw new Exception(ex.Message, ex);
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 利用Sql进行数据操作
 | ||
|         /// </summary>
 | ||
|         /// <param name="sql">sql语句</param>
 | ||
|         /// <returns>影响的行数</returns>
 | ||
|         public int ExecuteSql(String sql)
 | ||
|         {
 | ||
|             try
 | ||
|             {
 | ||
|                 Connection.Open();
 | ||
|                 OleDbCommand cmd = Connection.CreateCommand();
 | ||
|                 cmd.CommandText = sql;
 | ||
|                 int rtn = cmd.ExecuteNonQuery();
 | ||
|                 Connection.Close();
 | ||
|                 return rtn;
 | ||
|             }
 | ||
|             catch (Exception ex)
 | ||
|             {
 | ||
|                 if (Connection.State != ConnectionState.Closed)
 | ||
|                 {
 | ||
|                     Connection.Close();
 | ||
|                 }
 | ||
|                 throw new Exception(ex.Message, ex);
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 利用Sql进行数据操作
 | ||
|         /// </summary>
 | ||
|         /// <param name="sql">sql语句</param>
 | ||
|         /// <param name="param">执行参数</param>
 | ||
|         /// <returns>影响的行数</returns>
 | ||
|         public int ExecuteSql(String sql, params OleDbParameter[] param)
 | ||
|         {
 | ||
|             try
 | ||
|             {
 | ||
|                 Connection.Open();
 | ||
|                 OleDbCommand cmd = Connection.CreateCommand();
 | ||
|                 cmd.CommandText = sql;
 | ||
|                 cmd.Parameters.AddRange(param);
 | ||
|                 int rtn = cmd.ExecuteNonQuery();
 | ||
|                 Connection.Close();
 | ||
|                 return rtn;
 | ||
|             }
 | ||
|             catch (Exception ex)
 | ||
|             {
 | ||
|                 if (Connection.State != ConnectionState.Closed)
 | ||
|                 {
 | ||
|                     Connection.Close();
 | ||
|                 }
 | ||
|                 throw new Exception(ex.Message, ex);
 | ||
|             }
 | ||
|         }
 | ||
|     }
 | ||
| } |