這篇文章主要介紹了asp.net導出Excel類庫代碼,有需要的朋友可以參考一下
代碼如下: using System; using System.Collections.Generic; using System.Reflection; using System.Web; using Excel = Microsoft.Office.Interop.Excel; /// <summary> ///ExcelClass 的摘要說明 /// </summary> public class ExcelClass { /// <summary> /// 構建ExcelClass類 /// </summary> public ExcelClass() { this.m_objExcel = new Excel.Application(); } /// <summary> /// 構建ExcelClass類 /// </summary> /// <param name="objExcel">Excel.Application</param> public ExcelClass(Excel.Application objExcel) { this.m_objExcel = objExcel; } /// <summary> /// 列標號 /// </summary> private string AList = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; /// <summary> /// 獲取描述區域的字符 /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> public string GetAix(int x, int y) { char[] AChars = AList.ToCharArray(); if (x >= 26) { return ""; } string s = ""; s = s + AChars[x - 1].ToString(); s = s + y.ToString(); return s; } /// <summary> /// 給單元格賦值1 /// </summary> /// <param name="x">行號</param> /// <param name="y">列號</param> /// <param name="align">對齊(CENTER、LEFT、RIGHT)</param> /// <param name="text">值</param> public void setValue(int y, int x, string align, string text) { Excel.Range range = sheet.get_Range(this.GetAix(x, y), miss); range.set_Value(miss, text); if (align.ToUpper() == "CENTER") { range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter; } if (align.ToUpper() == "LEFT") { range.HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft; } if (align.ToUpper() == "RIGHT") { range.HorizontalAlignment = Excel.XlHAlign.xlHAlignRight; } } /// <summary> /// 給單元格賦值2 /// </summary> /// <param name="x">行號</param> /// <param name="y">列號</param> /// <param name="text">值</param> public void setValue(int y, int x, string text) { Excel.Range range = sheet.get_Range(this.GetAix(x, y), miss); range.set_Value(miss, text); } /// <summary> /// 給單元格賦值3 /// </summary> /// <param name="x">行號</param> /// <param name="y">列號</param> /// <param name="text">值</param> /// <param name="font">字符格式</param> /// <param name="color">顏色</param> public void setValue(int y, int x, string text, System.Drawing.Font font, System.Drawing.Color color) { this.setValue(x, y, text); Excel.Range range = sheet.get_Range(this.GetAix(x, y), miss); range.Font.Size = font.Size; range.Font.Bold = font.Bold; range.Font.Color = color; range.Font.Name = font.Name; range.Font.Italic = font.Italic; range.Font.Underline = font.Underline; } /// <summary> /// 插入新行 /// </summary> /// <param name="y">模板行號</param> public void insertRow(int y) { Excel.Range range = sheet.get_Range(GetAix(1, y), GetAix(25, y)); range.Copy(miss); range.Insert(Excel.XlDirection.xlDown, miss); range.get_Range(GetAix(1, y), GetAix(25, y)); range.Select(); sheet.Paste(miss, miss); } /// <summary> /// 把剪切內容粘貼到當前區域 /// </summary> public void past() { string s = "a,b,c,d,e,f,g"; sheet.Paste(sheet.get_Range(this.GetAix(10, 10), miss), s); } /// <summary> /// 設置邊框 /// </summary> /// <param name="x1"></param> /// <param name="y1"></param> /// <param name="x2"></param> /// <param name="y2"></param> /// <param name="Width"></param> public void setBorder(int x1, int y1, int x2, int y2, int Width) { Excel.Range range = sheet.get_Range(this.GetAix(x1, y1), miss); ((Excel.Range)range.Cells[x1, y1]).ColumnWidth = Width; } public void mergeCell(int x1, int y1, int x2, int y2) { Excel.Range range = sheet.get_Range(this.GetAix(x1, y1), this.GetAix(x2, y2)); range.Merge(true); } public Excel.Range getRange(int x1, int y1, int x2, int y2) { Excel.Range range = sheet.get_Range(this.GetAix(x1, y1), this.GetAix(x2, y2)); return range; } private object miss = Missing.Value; //忽略的參數OLENULL private Excel.Application m_objExcel;//Excel應用程序實例 private Excel.Workbooks m_objBooks;//工作表集合 private Excel.Workbook m_objBook;//當前操作的工作表 private Excel.Worksheet sheet;//當前操作的表格 public Excel.Worksheet CurrentSheet { get { return sheet; } set { this.sheet = value; } &