Quantcast
Channel: Open XML Format SDK forum
Viewing all articles
Browse latest Browse all 1288

How to optimize fetching of data from a Excel file which has more than 15k rows and only two columns.

$
0
0

Hi,

I am trying to create a dictionary from a Excel file with column 1 as Key and column 2 as Value.

I have used Open XML format,DOM method  to achieve the same but it's taking too much around 5 mins for creating a dictionary of length 15k.

Here is the code for the same.

private void refreshDictionaryOpenXML()
        {
            lblRefreshMessage.Text="Refreshing Dictionary,Please Wait...";
            btnTranslate.Enabled = false;

            string strDoc = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\dict1.xlsx";

            using (SpreadsheetDocument spreadsheetDocument =
                        SpreadsheetDocument.Open(strDoc, false))
            {
                WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
                WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();

                SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
                int str;
                int keyVal;

                try
                {

                    foreach (Row r in sheetData.Elements<Row>())
                    {
                        int colNo = 1;
                        string strString="";
                        string keyString = "";
                        foreach (Cell c in r.Elements<Cell>())
                        {
                            if (colNo==1)
                            {
                                if (c.CellValue != null && Int32.TryParse(c.InnerText, out str))
                                {
                                }
                                else
                                {
                                    str = -1;

                                }

                                if (str != -1)
                                {

                                    strString = GetSharedStringItemById(workbookPart, str);
                                    byte[] bytes = Encoding.Default.GetBytes(strString);
                                    byte[] devBytes = Encoding.Convert(Encoding.Default, encoding, bytes);
                                    strString = encoding.GetString(devBytes);

                                }

                            }
                            else if(colNo==2)
                            {
                                if (c.CellValue != null && Int32.TryParse(c.InnerText, out keyVal))
                                {
                                }
                                else
                                {
                                    keyVal = -1;

                                }

                                if (keyVal != -1)
                                {
                                    keyString = GetSharedStringItemById(workbookPart, keyVal);

                                }
                            }
                            colNo += 1;
                        }

                        if (!translatorDictionary.ContainsKey(strString))
                        {
                            translatorDictionary.Add(strString, keyString);

                        }

                    }
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            lblRefreshMessage.Text = "Dictionary Refreshed";
            btnTranslate.Enabled = true;
        }

Please let me know how can this be optimized more for faster operation.


Point5Nyble


Viewing all articles
Browse latest Browse all 1288

Trending Articles