खोज…


परिचय

यह POI Java प्रोजेक्ट का .NET वर्जन है। यह Microsoft Office स्थापित किए बिना xls, doc, ppt फ़ाइलों को पढ़ने / लिखने की अनुमति देता है। प्रलेखन के बारे में विवरण यहाँ उपलब्ध है: https://github.com/tonyqus/npoi

एनपीओआई स्थापित करना

NPOI से संबंधित सभी लाइब्रेरी को शामिल करने का सबसे अच्छा तरीका NUGet पैकेज मैनेजर है। NUGet पैकेज प्रबंधक विंडो पर NPOI के लिए खोजें।

यहाँ छवि विवरण दर्ज करें

एक बार जब यह सफलतापूर्वक स्थापित हो जाता है तो सभी आवश्यक पुस्तकालय आपके वर्तमान प्रोजेक्ट के संदर्भ अनुभाग में दिखाई देंगे यहाँ छवि विवरण दर्ज करें

फिर NPOI को अपनी फ़ाइल में इस तरह शामिल करें

using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.XSSF.UserModel

एक एक्सेल फ़ाइल बनाएँ

MemoryStream excelMS =  GetExcelFile();
 
 //Using Resposne Stream to Make File Available for User to Download;
 Response.Clear();
 Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
 Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", @"Excel_" + DateTime.Now.ToString("yyyy-dd-M-HH-mm") + ".xlsx"));

 Response.BinaryWrite(excelMS.ToArray());
 Response.End();

MIME प्रकार का उपयोग करते समय सावधान रहें। आपने विभिन्न फ़ाइल स्वरूपों के लिए विभिन्न MIME प्रकारों का चयन किया होगा। EX xltm विस्तार के लिए यह "आवेदन / vnd.ms-excel.template.macroEnabled.12" होगा; xlxs एक्सटेंशन यह "एप्लिकेशन / vnd.openxmlformats-officedocument.spreadsheetml.sheet" होगा; विभिन्न MIME प्रकार समर्थित MS इस लिंक पर है

public MemoryStream GetExcelFile()
{
    //Excel File Stream 
    MemoryStream ms = null;
    // create workbook
    XSSFWorkbook workbook = new XSSFWorkbook();
    // the table named mySheet
    //Assigning New Sheet Name /
    XSSFSheet sheet = (XSSFSheet)workbook.CreateSheet("WorkSheet");
       
    // Assuming FreezePaneRow  = 10, FreezePaneColumn = 11 in the config file
    int freezeRow = Convert.ToInt32(ConfigurationManager.AppSettings["FreezePaneRow"]);
    int freezeCol = Convert.ToInt32(ConfigurationManager.AppSettings["FreezePaneCol"]);
    try
    {
        // Freeze Created Excel Sheet Row;
            sheet.CreateFreezePane(freezeCol, freezeRow, freezeCol, freezeRow);
        //Freezing only the Header Row;
            sheet.CreateFreezePane(0, 1);

         using (ms = new MemoryStream())
            {
                logger.Info("Using Memory Stream to Create New WorkBook");
                workbook.Write(ms); // Write to memory stream for download through browser     
            }
    }
    catch (Exception Ex)
    { ... }
    return ms;
}

एक्सेल फाइल पढ़ना

एक्सेल फाइल को पढ़ने के लिए विभिन्न तरीके हैं। मैं फ़ाइल पथ पैरामीटर के साथ एक उदाहरण प्रदान करता हूँ। आप इस पोस्ट पर फ़ाइल पढ़ने के लिए विभिन्न तरीके प्राप्त कर सकते हैं।

public void ReadExcel(string path)
        {
            // Write data in workbook from xls document.
            XSSFWorkbook workbook = new XSSFWorkbook(path);
            // Read the current table data
            XSSFSheet sheet = (XSSFSheet)workbook.GetSheetAt(0);
            // Read the current row data
            XSSFRow headerRow = (XSSFRow)sheet.GetRow(0);
            // LastCellNum is the number of cells of current rows
            int cellCount = headerRow.LastCellNum;
            DataTable dt = new DataTable();
            bool isBlanKRow = false;

            try
            {
                if (dt.Rows.Count == 0)
                {
                    //Reading First Row as Header for Excel Sheet;
                    try
                    {
                        for (int j = headerRow.FirstCellNum; j < cellCount; j++)
                        {
                            // get data as the column header of DataTable
                            DataColumn column = new DataColumn(headerRow.GetCell(j).StringCellValue);
                            dt.Columns.Add(column);
                        }
                    }
                    catch (Exception Ex)
                    { }
                }

                for (int sheetindex = 0; sheetindex < workbook.NumberOfSheets; sheetindex++)
                {
                    sheet = (XSSFSheet)workbook.GetSheetAt(sheetindex);
                    if (null != sheet)
                    {
                        // LastRowNum is the number of rows of current table
                        int rowCount = sheet.LastRowNum + 1;
                        //Reading Rows and Copying it to Data Table;
                        try
                        {
                            for (int i = (sheet.FirstRowNum + 1); i < rowCount; i++)
                            {
                                XSSFRow row = (XSSFRow)sheet.GetRow(i);
                                DataRow dataRow = dt.NewRow();
                                isBlanKRow = true;
                                try
                                {
                                    for (int j = row.FirstCellNum; j < cellCount; j++)
                                    {
                                        if (null != row.GetCell(j) && !string.IsNullOrEmpty(row.GetCell(j).ToString()) && !string.IsNullOrWhiteSpace(row.GetCell(j).ToString()))
                                        {
                                            dataRow[j] = row.GetCell(j).ToString();
                                            isBlanKRow = false;
                                        }

                                    }
                                }
                                catch (Exception Ex)
                                { }
                                if (!isBlanKRow)
                                {
                                    dt.Rows.Add(dataRow);
                                }
                            }
                        }
                        catch (Exception Ex)
                        { }
                    }
                }
            }
            catch (Exception Ex)
            { }
            finally
            {
                workbook.UnlockStructure();
                workbook.UnlockRevision();
                workbook.UnlockWindows();
                workbook = null;
                sheet = null;
            }
        }


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow