0

Good day.

I am newbie in coding and need your help.

I am trying to create an app which will.

  1. Browse all doc, XLS and ppt files through browse button and will show the file names in TextBox1.
  2. When I will click Convert Button, it will grab files from TextBox1 and will convert doc to docs, XLS to XLSX and ppt to PPTX.

I am successful till browsing files and showing all files in TextBox1, but unable to get/select files from TextBox1 for conversion.

What am I looking for?

  1. Get files which are stored in TextBox1 in Convert Button code.
  2. Convert these files to the relevant new format.
  3. Show data in loading bar/ Progress Bar so that I can have an idea how long the conversion takes.

What I tried so far from different forums is shared below

using System;
using System.Collections.Generic;
//using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Spire.Doc;
using Spire.Xls;
using Spire.Presentation;
using Microsoft.Office.Interop.Excel;

namespace T_Converter
{
    public partial class Form1 : Form
    {
        private object workbook;

        public object ExcelVersion { get; private set; }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            BTNClear.Enabled = false;
            BTNConvert.Enabled = false;
        }

        private void BTNBrowse_Click(object sender, EventArgs e)
        {
            var dialog = new OpenFileDialog();
            dialog.InitialDirectory = @"\Desktop";
            dialog.Title = "Browse old office files";
           // dialog.Filter = "Office Files (*.xls)|*.xls";
            dialog.DefaultExt = ".xls";

            
            //OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = true;
           

            var result = dialog.ShowDialog();

            foreach (string file in dialog.FileNames)
            {
                textBox1.AppendText(Path.GetFileName(file) + Environment.NewLine);
            }

            
            BTNConvert.Enabled = true;
                BTNClear.Enabled = true;

        }

        private void BTNClear_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            BTNClear.Enabled = false;
            BTNConvert.Enabled = false;
        }

        private void BTNClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BTNConvert_Click(object sender, EventArgs e)
        {
            
        }
    }

            //Create a Workbook instance
            
            namespace ConvertXlsToXlsx

    {

        class Program

        {

            static void main(string[] args)

            {

                //Create a Workbook instance

                Excel.Workbook workbook = new Excel.Workbook();

                //Load an XLS file

                Workbook.LoadFromFile(@"\Desktop\Test.xlsx");



                //Convert the file to XLSX format

                workbook.SaveToFile(@"C:\Users\Test\Desktop\test.xlsx", ExcelVersion.Version2016);
                MessageBox.Show("OK");
            }

        }

    }

}
    
Fildor
  • 14,510
  • 4
  • 35
  • 67
  • These are 3 questions in one. Can you narrow down to one at a time? Looks like it makes sense to solve them in order 1 through 3 anyway. – Fildor Jul 28 '23 at 08:31
  • To start with question1: `textBox1.AppendText(Path.GetFileName(file) + Environment.NewLine);` you should actually keep the full paths in a Model and bind it to the TextBox, so changes in the Model are reflected there, you can control how they are presented _and_ you have a convenient list of full paths to pass to your processing code. – Fildor Jul 28 '23 at 08:35
  • That additional `main` method won't fly though. You can only have one in your Application. You still can make classes for each conversion "type" (xls => xlsx, doc => docx ...) – Fildor Jul 28 '23 at 08:37
  • _"3. Show data in loading bar/ Progress Bar so that I can have an idea how long the conversion takes."_ - you might also want to consider No 4: provide a "cancel" Button to abort the conversion process. – Fildor Jul 28 '23 at 08:43
  • Hi @Fildor-standswithMods Thank you so much for your quick response. Can you please explain with the help of code examples? – Aamir Karim Jul 28 '23 at 09:07
  • Well that's a bit outside the scope for an answer. But you can look for e.g. [MVC in WinForms](https://www.codeproject.com/articles/383153/the-model-view-controller-mvc-pattern-with-csharp) – Fildor Jul 28 '23 at 09:44

0 Answers0