使用NPOI按照模版导出导入excle表

来源:互联网 发布:金益康人事软件 编辑:程序博客网 时间:2024/06/03 12:00

1 添加NPOI相关引用

2 在debug下建立一个excle模版文件

3

导入

private void Createcell(IRow row, int index, string msg)
        {
            ICell ic = row.GetCell(index);
            if (msg != null)
            {
                ic.SetCellValue(msg);
            }

        }
        private void button2_Click(object sender, EventArgs e)
        {
            string excelpath ="c:/11" + ".xls";
            File.Copy(Application.StartupPath + "\\temp.xls", excelpath, true);

            HSSFWorkbook writexcel;
            using (FileStream file = new FileStream(excelpath, FileMode.Open, FileAccess.Read, System.IO.FileShare.Read))
            {
                writexcel = new HSSFWorkbook(file);
            }

            //打开sheet
            ISheet sheet = writexcel.GetSheetAt(0);
            Createcell(sheet.GetRow(1), 6,"qunimade");
            using (FileStream fileStream = File.Open(excelpath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                writexcel.Write(fileStream);
                fileStream.Close();
            }
        }

导出

private void   Readcell(IRow row, int index)
        {
            ICell ic = row.GetCell(index);
          
                return  ic.StringCellValue;
       

        }

  string excelpath ="c:/11" + ".xls";
            HSSFWorkbook writexcel;
            using (FileStream file = new FileStream(excelpath, FileMode.Open, FileAccess.Read, System.IO.FileShare.Read))
            {
              readxcel = new HSSFWorkbook(file);
            }

            //打开sheet
            ISheet sheet = writexcel.GetSheetAt(0);
      string result=     Readcell(sheet.GetRow(1), 6);


注意  格式转换代码

  1. switch (cell.CellType)  
  2.                                             {  
  3.                                                 case CellType.Blank:  
  4.                                                     dataRow[j] = "";  
  5.                                                     break;  
  6.                                                 case CellType.Numeric:  
  7.                                                     short format = cell.CellStyle.DataFormat;  
  8.                                                     //对时间格式(2015.12.5、2015/12/5、2015-12-5等)的处理  
  9.                                                     if (format == 14 || format == 31 || format == 57 || format == 58)  
  10.                                                         dataRow[j] = cell.DateCellValue;  
  11.                                                     else  
  12.                                                         dataRow[j] = cell.NumericCellValue;  
  13.                                                     break;  
  14.                                                 case CellType.String:  
  15.                                                     dataRow[j] = cell.StringCellValue;  
  16.                                                     break;  
  17.                                             } 

0 0