Can I change the format and extension of an excel file in C #?

0

I currently have this fraction of code:

saveFileDialog.InitialDirectory = "C:/Escritorio";
saveFileDialog.Title = "Guardar Archivo en Excel";
saveFileDialog.FileName = "";
saveFileDialog.Filter = "CSV Files (*.csv)|*.csv";

But when I open the file it tells me that the format and the extension do not match, I'm working with C # and DevExpress.

I appreciate all kinds of comments to be able to solve the problem.

This is the complete code:

private void ExceltoolStripButton_Click(object sender, EventArgs e)
    {
        saveFileDialog.InitialDirectory = "C:/Escritorio";
        saveFileDialog.Title = "Guardar Archivo en Excel";
        saveFileDialog.FileName = "";
        saveFileDialog.Filter = "CSV Files (*.csv)|*.csv";

        if (saveFileDialog.ShowDialog() != DialogResult.Cancel)
        {
            Excel.Application MExcel = new Excel.Application();
            MExcel.Application.Workbooks.Add(Type.Missing);
            for (int i = 1; i < encargadosDataGridView.Columns.Count + 1; i++)
            {
                MExcel.Cells[1, i] = encargadosDataGridView.Columns[i - 1].HeaderText;
            }
            for (int i = 0; i < encargadosDataGridView.Rows.Count; i++)
            {
                for (int j = 0; j < encargadosDataGridView.Columns.Count; j++)
                {
                    MExcel.Cells[i + 2, j + 1] = encargadosDataGridView.Rows[i].Cells[j].Value.ToString();
                }
            }
            MExcel.ActiveWorkbook.SaveCopyAs(saveFileDialog.FileName.ToString());
            MExcel.ActiveWorkbook.Saved = true;
            MExcel.Quit();
        }
    }

Full Code:

private void ExceltoolStripButton_Click(object sender, EventArgs e)
    {
        saveFileDialog.InitialDirectory = "C:/Escritorio";
        saveFileDialog.Title = "Guardar Archivo en Excel";
        saveFileDialog.FileName = "";
        saveFileDialog.Filter = "CSV Files (*.csv)|*.csv";

        if (saveFileDialog.ShowDialog() != DialogResult.Cancel)
        {
            Excel.Application MExcel = new Excel.Application();
            MExcel.Application.Workbooks.Add(Type.Missing);
            for (int i = 1; i < encargadosDataGridView.Columns.Count + 1; i++)
            {
                MExcel.Cells[1, i] = encargadosDataGridView.Columns[i - 1].HeaderText;
            }
            for (int i = 0; i < encargadosDataGridView.Rows.Count; i++)
            {
                for (int j = 0; j < encargadosDataGridView.Columns.Count; j++)
                {
                    MExcel.Cells[i + 2, j + 1] = encargadosDataGridView.Rows[i].Cells[j].Value.ToString();
                }
            }
            MExcel.ActiveWorkbook.SaveCopyAs(saveFileDialog.FileName.ToString());
            MExcel.ActiveWorkbook.Saved = true;
            MExcel.Quit();
        }
    }
    
asked by Joaquin Villegas Trujillo 04.08.2018 в 20:53
source

0 answers