程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#ͨ��NPOI����Excel��ʵ������

C#ͨ��NPOI����Excel��ʵ������

編輯:C#入門知識

C#ͨ��NPOI����Excel��ʵ������。本站提示廣大學習愛好者:(C#ͨ��NPOI����Excel��ʵ������)文章只能為提供參考,不一定能成為您想要的結果。以下是C#ͨ��NPOI����Excel��ʵ������正文


C#����Excel�ķ����кܶ��֣���������΢��ٷ���OLE Automation��Apache��POI�ȡ�������ܵ���POI�����C#��NPOI��

POI��Apache��ͨ��Java����Office��һ��API�����Զ�Excel��Word��PPT�Ƚ��в�����ʮ�ֵ�ǿ��Ȼ��ͱ������C#�汾��NPOI�ˣ���log4j��log4net�����ơ�

������NPOI��.net4.0�汾֮ǰ�Dz�֧��office2007�����ϵ�XML��ʽ�ģ��������µİ汾�Ѿ�֧���ˡ�ֻ��Ҫ���ز���������������򼯾���ʹ���ˡ�

�����ṩһ������Excel���࣬�����ṩ��4�������������������������롣����ͨ��DataSet����ӵ�ж��Sheet��Excel�ļ���Ҳ����ͨ��DataTable����ӵ��һ��Sheet��Excel������Ҳ��һ����ͨ��ָ��Sheet����������DataTable������ֱ�ӵ�������Sheet����һ��DataSet��

public class ExcelHelper
  {
    /// <summary>
    /// ����Excel��Sheet����DataTable
    /// </summary>
    /// <param name="filePath">Excel�ļ���ַ</param>
    /// <param name="sheetIndex">Sheet����</param>
    /// <returns>DataTable</returns>
    public static DataTable GetDataTable(string filePath, int sheetIndex)
    {
      return GetDataSet(filePath, sheetIndex).Tables[0];
    }

    /// <summary>
    /// ����Excel����DataSet
    /// </summary>
    /// <param name="filePath">Excel�ļ���ַ</param>
    /// <param name="sheetIndex">Sheet��������ѡ��Ĭ�Ϸ�������Sheet</param>
    /// <returns>DataSet</returns>
    public static DataSet GetDataSet(string filePath, int? sheetIndex = null)
    {
      DataSet ds = new DataSet();
      IWorkbook fileWorkbook;
      using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
      {
        if (filePath.Last() == 's')
        {
          try
          {
            fileWorkbook = new HSSFWorkbook(fs);
          }
          catch (Exception ex)
          {
            throw ex;
          }
        }
        else
        {
          try
          {
            fileWorkbook = new XSSFWorkbook(fs);
          }
          catch
          {
            fileWorkbook = new HSSFWorkbook(fs);
          }
        }
      }

      for (int i = 0; i < fileWorkbook.NumberOfSheets; i++)
      {
        if (sheetIndex != null && sheetIndex != i)
          continue;
        DataTable dt = new DataTable();
        ISheet sheet = fileWorkbook.GetSheetAt(i);

        //��ͷ
        IRow header = sheet.GetRow(sheet.FirstRowNum);
        List<int> columns = new List<int>();
        for (int j = 0; j < header.LastCellNum; j++)
        {
          object obj = GetValueTypeForXLS(header.GetCell(j) as HSSFCell);
          if (obj == null || obj.ToString() == string.Empty)
          {
            dt.Columns.Add(new DataColumn("Columns" + j.ToString()));
          }
          else
            dt.Columns.Add(new DataColumn(obj.ToString()));
          columns.Add(j);
        }
        //����
        IEnumerator rows = sheet.GetEnumerator();
        while (rows.MoveNext())
        {
          int j = sheet.FirstRowNum + 1;
          DataRow dr = dt.NewRow();
          bool hasValue = false;
          foreach (int K in columns)
          {
            dr[K] = GetValueTypeForXLS(sheet.GetRow(K).GetCell(K) as HSSFCell);
            if (dr[K] != null && dr[K].ToString() != string.Empty)
            {
              hasValue = true;
            }
          }
          if (hasValue)
          {
            dt.Rows.Add(dr);
          }
          j++;
        }
        ds.Tables.Add(dt);
      }

      return ds;
    }

    /// <summary>
    /// ����DataTable����Excel
    /// </summary>
    /// <param name="dt">DataTable</param>
    /// <param name="file">�����ַ</param>
    public static void GetExcelByDataTable(DataTable dt, string file)
    {
      DataSet ds = new DataSet();
      ds.Tables.Add(dt);
      GetExcelByDataSet(ds, file);
    }

    /// <summary>
    /// ����DataSet����Excel
    /// </summary>
    /// <param name="ds">DataSet</param>
    /// <param name="file">�����ַ</param>
    public static void GetExcelByDataSet(DataSet ds, string file)
    {
      IWorkbook fileWorkbook = new HSSFWorkbook();
      int index = 0;
      foreach (DataTable dt in ds.Tables)
      {
        index++;
        ISheet sheet = fileWorkbook.CreateSheet("Sheet" + index);

        //��ͷ
        IRow row = sheet.CreateRow(0);
        for (int i = 0; i < dt.Columns.Count; i++)
        {
          ICell cell = row.CreateCell(i);
          cell.SetCellValue(dt.Columns[i].ColumnName);
        }

        //����
        for (int i = 0; i < dt.Rows.Count; i++)
        {
          IRow row1 = sheet.CreateRow(i + 1);
          for (int j = 0; j < dt.Columns.Count; j++)
          {
            ICell cell = row1.CreateCell(j);
            cell.SetCellValue(dt.Rows[i][j].ToString());
          }
        }
      }

      //תΪ�ֽ�����
      MemoryStream stream = new MemoryStream();
      fileWorkbook.Write(stream);
      var buf = stream.ToArray();

      //����ΪExcel�ļ�
      using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write))
      {
        fs.Write(buf, 0, buf.Length);
        fs.Flush();
      }
    }

    /// <summary>
    /// ���ݵ�Ԫ�����ݷ���Ϊ��Ӧ���͵�����
    /// </summary>
    /// <param name="cell">��Ԫ��</param>
    /// <returns>����</returns>
    private static object GetValueTypeForXLS(HSSFCell cell)
    {
      if (cell == null)
        return null;
      switch (cell.CellType)
      {
        case CellType.Blank: //BLANK:
          return null;
        case CellType.Boolean: //BOOLEAN:
          return cell.BooleanCellValue;
        case CellType.Numeric: //NUMERIC:
          return cell.NumericCellValue;
        case CellType.String: //STRING:
          return cell.StringCellValue;
        case CellType.Error: //ERROR:
          return cell.ErrorCellValue;
        case CellType.Formula: //FORMULA:
        default:
          return "=" + cell.CellFormula;
      }
    }
  }

�����������һЩ����˼�IJ���������汾�������⡣����ͨ����̬�ܺõ�ʵ���˼��ݣ����������2007�汾��xlsm���޸�Ϊxsl�ĺ�׺��ô���أ�����2003�汾�ı��޸�Ϊxlsm��׺��ô���ء�2003�汾��Ϊxlsm���ǿ��Խ�����Ϊxls������ģ�����2007��Ϊxls�Ͳ����ˡ���ʱ�����ǿ���޸��ļ��ĺ�׺���ٴ򿪡�

��������Ĵ���û��ʵ��������ܣ�����ԭ��һ�����������Ǻܰ�ȫ��������ʱ����Ҫ�޸�ϵͳ�������ط����ļ����ļ�������������ʵ�ֲ��Ǻܷ��㡣

���Ͼ��DZ��ĵ�ȫ�����ݣ�ϣ���Դ�ҵ�ѧϰ����������Ҳϣ����Ҷ��֧�ֽű�֮�ҡ�

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved