2 Replies - 423 Views - Last Post: 20 April 2016 - 03:25 AM Rate Topic: -----

#1 zeusien  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-April 16

create a function that will allow parser

Posted 20 April 2016 - 02:22 AM

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace WpfApplication1
{
    /// <summary>
    /// Logique d'interaction pour Mainwindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
        }

        private void button_click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fileD = new OpenFileDialog();
            fileD.Filter = "Excel files|*.xls;*.xlsx";
            fileD.Multiselect = false;

            if (fileD.ShowDialog() == true)
            {
                string sFileName = fileD.FileName;
                using (SpreadsheetDocument spreadsheetDocument =
                       Spreadsheetdocument.Open(fileD.FileName, false))
                {
                    WorkbookPart workbookPart = spreadsheetdocument.WorkbookPart;
                    WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
                    SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();


                    foreach (Row r in sheetData.Elements<Row>())

                    {
                        foreach (Cell c in r.Elements<Cell>())
                            MonExcel(workbookPart, c);
                    }
                }
          }
       }
       private static void MonExcel(WorkbookPart workbookPart, Cell c)
        {
            string value = null;

            if (c.CellValue != null && !String.IsNullOrEmpty(c.CellValue.Text))
            {
                value = c.InnerText;
                if (c.DataType != null)
                {
                    switch (c.DataType.Value)
                    {
                        case CellValues.SharedString:
                            var stringTable =
                            workbookPart.GetPartsOfType<SharedStringTablePart>()
                            .FirstOrDefault();
                            if (stringTable != null)
                            {
                                value =
                                stringTable.SharedStringTable
                                .ElementAt(int.Parse(value)).InnerText;
                            }
                            break;

                        case CellValues.Boolean:
                            switch (value)
                            {
                                case "0":
                                    value = "FALSE";
                                    break;
                                default:
                                    value = "TRUE";
                                    break;
                            }
                            break;
                    }
                }
            }
        }
    }
}




Hello, I need to create a function that will retrieve the "key" value in an Excel file, how I do

Is This A Good Question/Topic? 0
  • +

Replies To: create a function that will allow parser

#2 andrewsw  Icon User is offline

  • say what now
  • member icon

Reputation: 6410
  • View blog
  • Posts: 25,910
  • Joined: 12-December 12

Re: create a function that will allow parser

Posted 20 April 2016 - 02:42 AM

What "key" value?
Was This Post Helpful? 0
  • +
  • -

#3 zeusien  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-April 16

Re: create a function that will allow parser

Posted 20 April 2016 - 03:25 AM

View Postandrewsw, on 20 April 2016 - 02:42 AM, said:

What "key" value?


using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace WpfApplication1
{
    /// <summary>
    /// Logique d'interaction pour Mainwindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
        }

        private void button_click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fileD = new OpenFileDialog();
            fileD.Filter = "Excel files|*.xls;*.xlsx";
            fileD.Multiselect = false;

            if (fileD.ShowDialog() == true)
            {
                string sFileName = fileD.FileName;
                using (SpreadsheetDocument spreadsheetDocument =
                       Spreadsheetdocument.Open(fileD.FileName, false))
                {   

                    WorkbookPart workbookPart = spreadsheetdocument.WorkbookPart;
                    WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
                    SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();


                    foreach (Row r in sheetData.Elements<Row>())
                    {
                        var cells = r.Elements<Cell>();
                        if (MonParcours(workbookPart, cells.ElementAt(0)) == "Key")
                        {
                           ----> table or list
                        }
                    }
                }
          }
       }
       private static string MonParcours(WorkbookPart workbookPart, Cell c)
        {
            string value = null;

            if (c.CellValue != null && !String.IsNullOrEmpty(c.CellValue.Text))
            {
                value = c.InnerText;
                if (c.DataType != null)
                {
                    switch (c.DataType.Value)
                    {
                        case CellValues.SharedString:
                            var stringTable =
                            workbookPart.GetPartsOfType<SharedStringTablePart>()
                            .FirstOrDefault();
                            if (stringTable != null)
                            {
                                value =
                                stringTable.SharedStringTable
                                .ElementAt(int.Parse(value)).InnerText;
                            }
                            break;

                        case CellValues.Boolean:
                            switch (value)
                            {
                                case "0":
                                    value = "FALSE";
                                    break;
                                default:
                                    value = "TRUE";
                                    break;
                            }
                            break;
                    }
                }
            }

            return value;
        }
    }
}




I was able to recover the cells or is a "key", now I have to store in a table or a list, cells that are "key", but I see how, in the if.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1