C# WPF는 NPOI 어셈블리를 통해 Excel 컨텐트를 가져오고 컴파일합니다.

14712 단어 C#c#excel

NPOI 대여.Excel 구성 요소 가져오기 및 편집 지정한 Excel 내용

  • 내용 얻기
  • 내용을 편집합니다

  • 컨텐츠 가져오기

            private void ReadExcel()
            {
                string strReadFilePath = string.Empty;
                Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
                dlg.Filter = "All File|*.*|Excel documents|*.xlsx|Excel(97 - 2003)documents|*.xls"; 
                Nullable<bool> result = dlg.ShowDialog();
                if (result == true)
                {
                    strReadFilePath = dlg.FileName;
                }
                else
                    return;
                System.IO.FileStream fs = new System.IO.FileStream(strReadFilePath, System.IO.FileMode.Open,System.IO.FileAccess.Read);
                IWorkbook workbook = WorkbookFactory.Create(fs);
                // 
                int nSheets = workbook.NumberOfSheets;
                for (int i = 0; i < nSheets; i++)
                {
                    // 
                    string strSheetName = workbook.GetSheetName(i);
                    if (i != 0)
                    {
                        //1. Excel  i  Sheet 
                        ISheet sheet = workbook.GetSheetAt(i);
                        // 
                        int nRowsCount = sheet.LastRowNum + 1;
                        string strAim = "0000";
                        for (int k = 0; k < nRowsCount; k++)
                        {
                            //  I 
                            IRow row = sheet.GetRow(k);
                            // I   0 
                            ICell cell = row.GetCell(0);
                            strAim += string.Format(",{0}", cell.ToString());
                        }                
                    }
                }
            }
    

    컨텐트 편집

            private void WriteExcel()
            {
                Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
                dlg.Filter = "Excel documents|*.xlsx|Excel(97 - 2003)documents|*.xls";
                bool? result = dlg.ShowDialog();
                if (result == true)
                    strWriteFilePath = dlg.FileName;
                else
                    return;
    
                System.IO.FileStream fs = new System.IO.FileStream(strWriteFilePath, System.IO.FileMode.Create);
    
                IWorkbook workbook;
                if (strWriteFilePath.Contains(".xls"))
                    workbook = new NPOI.HSSF.UserModel.HSSFWorkbook();
                else if (strWriteFilePath.Contains(".xlsx"))
                    workbook = new NPOI.XSSF.UserModel.XSSFWorkbook();
                else
                    return;
    
                int nRow = 5;
                int nCell = 30;
    
                // 
                ISheet sheet = workbook.CreateSheet();
                //  nRow  
                IRow row = sheet.CreateRow(nRow);
                //  nRow     nCell    “ ” 
                row.CreateCell(nCell).SetCellValue(" ");
                // 
                workbook.Write(fs);
                //  
                fs.Close();
            }
    
    

    좋은 웹페이지 즐겨찾기