Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,

I am try to retrieve data from my C# Windows application to an Excel spreadsheet and store this.xls file to a desired location. My problem is that my code saves the excel file to a predefined location.
private Excel.Application m_objExcel = null;
private Excel.Workbooks m_objBooks = null;
private Excel._Workbook m_objBook = null;
private Excel.Sheets m_objSheets = null;
private Excel._Worksheet m_objSheet = null;
private Excel.Range m_objRange = null;
private Excel.Font m_objFont = null;
private Excel.QueryTables m_objQryTables = null;
private Excel._QueryTable m_objQryTable = null;
// Frequenty-used variable for optional arguments.
private object m_objOpt = System.Reflection.Missing.Value;

// Paths used by the sample code for accessing and storing data.
private object m_strSampleFolder = Application.StartupPath  + "\\ExcelData\\" ;

private void xlsheet()
{
    // Start a new workbook in Excel.

    m_objExcel = new Excel.Application();
    m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
    m_objBook = (Excel._Workbook)(m_objBooks.Add(m_objOpt));
    m_objSheets = (Excel.Sheets)m_objBook.Worksheets;
    m_objSheet = (Excel._Worksheet)(m_objSheets.get_Item(1));
    // Create an array for the headers and add it to cells A1:C1.
    object[] objHeaders = { "X-Axis CH1", "Y-Axis CH1","Nilesh", "X-Axis CH2", "Y-Axis CH2" };
    m_objRange = m_objSheet.get_Range("A1", "E1");
    m_objRange.set_Value(m_objOpt, objHeaders);
    m_objFont = m_objRange.Font;
    m_objFont.Bold = true;
    // Create an array with 3 columns and 100 rows and add it to
    // the worksheet starting at cell A2.
    object[,] objData = new Object[bufn.Length, 5];
    Int64 c_xl1 = 0, c_xl2=0;
    for (int r = 0; r < bufn.Length; r++)
    {
        objData[r, 0] = c_xl1.ToString();
        objData[r, 1] = (bufn[r] + vsc2).ToString();
        c_xl1 += 1;
        objData[r, 3] = c_xl2.ToString();
        objData[r, 4] = (bufb[r] + vsc4).ToString();
        c_xl2 += 1;
    }
    m_objRange = m_objSheet.get_Range("A2", m_objOpt);
    m_objRange = m_objRange.get_Resize(bufn.Length, 5);
    m_objRange.set_Value(m_objOpt, objData);
    // Save the workbook and quit Excel.
   m_objBook.SaveAs(m_strSampleFolder+nm.ToString()+"Book24.xlsx", m_objOpt, m_objOpt,
        m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange,
        m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt);
    m_objBook.Close(false, m_objOpt, m_objOpt);
    m_objExcel.Quit();
    nm += 1;
}

C#
private void iFormGlassButton3_Click(object sender, EventArgs e)
        {
            xlsheet();
        }

This code saves file at predefine location
private object m_strSampleFolder = Application.StartupPath  + "\\ExcelData\\;

Now I am trying to use the SaveFileDialog control which saves the file to a desired location like this
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
            saveFileDialog1.Title = "Save an excel File";
            saveFileDialog1.DefaultExt = "xls";
            saveFileDialog1.ShowDialog();

            // If the file name is not an empty string open it for saving.
            if (saveFileDialog1.FileName != "")
            {
                // Saves the Image via a FileStream created by the OpenFile method.
                System.IO.FileStream fs =
                   (System.IO.FileStream)saveFileDialog1.OpenFile();
                // Saves the Image in the appropriate ImageFormat based upon the
                // File type selected in the dialog box.
                // NOTE that the FilterIndex property is one-based.


                fs.Close();
}


both functions work properly but my requirement is that I want to store the Excel spreadsheet to a desired location.

Please help me. Can i use the FolderBrowserDialog and how can I use with my code?

Thanks and Regards
Nilesh
Posted
Updated 3-Mar-11 1:14am
v5
Comments
Piccadilly Yum Yum 3-Mar-11 5:46am    
what's the problem ?
NileshKRathod 3-Mar-11 5:54am    
probllem is that both of process work separately.
I want to retriev data from xlsheet() function and save this excel file with the help of SaveFileDialog.
it means user can store this file at any location which is possible in SaveFileDialog.
now you get it ?

1 solution

Its Ok I am success to solve this problem... :) :)
with the help of lt FolderBrowserDialog We can open file dialog and send this path to private object m_strSampleFolder = Application.StartupPath + "\\ExcelData\\" ;
Like This
private Excel.Application m_objExcel = null;
        private Excel.Workbooks m_objBooks = null;
        private Excel._Workbook m_objBook = null;
        private Excel.Sheets m_objSheets = null;
        private Excel._Worksheet m_objSheet = null;
        private Excel.Range m_objRange = null;
        private Excel.Font m_objFont = null;
        private Excel.QueryTables m_objQryTables = null;
        private Excel._QueryTable m_objQryTable = null;
        string path11;
        // Frequenty-used variable for optional arguments.
        private object m_objOpt = System.Reflection.Missing.Value;
        // Paths used by the sample code for accessing and storing data.
        private object m_strSampleFolder ;
        private void Button3_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                this.textBox3.Text = folderBrowserDialog1.SelectedPath;
            }
            path11 = textBox3.Text;
            m_strSampleFolder = path11.ToString()+ "\\" ";
            xlsheet();
        }
        private void xlsheet()
        {
            // Start a new workbook in Excel.
            
            m_objExcel = new Excel.Application();
            m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
            m_objBook = (Excel._Workbook)(m_objBooks.Add(m_objOpt));
            m_objSheets = (Excel.Sheets)m_objBook.Worksheets;
            m_objSheet = (Excel._Worksheet)(m_objSheets.get_Item(1));
            // Create an array for the headers and add it to cells A1:C1.
            object[] objHeaders = { "X-Axis CH1", "Y-Axis CH1","Nilesh", "X-Axis CH2", "Y-Axis CH2" };
            m_objRange = m_objSheet.get_Range("A1", "E1");
            m_objRange.set_Value(m_objOpt, objHeaders);
            m_objFont = m_objRange.Font;
            m_objFont.Bold = true;
            // Create an array with 3 columns and 100 rows and add it to
            // the worksheet starting at cell A2.
            object[,] objData = new Object[bufn.Length, 5];
            Int64 c_xl1 = 0, c_xl2=0;
            for (int r = 0; r &amp;lt; bufn.Length; r++)
            {
                objData[r, 0] = c_xl1.ToString();
                objData[r, 1] = (bufn[r] + vsc2).ToString();
                c_xl1 += 1;
                objData[r, 3] = c_xl2.ToString();
                objData[r, 4] = (bufb[r] + vsc4).ToString();
                c_xl2 += 1;
            }
            m_objRange = m_objSheet.get_Range("A2", m_objOpt);
            m_objRange = m_objRange.get_Resize(bufn.Length, 5);
            m_objRange.set_Value(m_objOpt, objData);
            // Save the workbook and quit Excel.
            m_objBook.SaveAs(m_strSampleFolder + nm.ToString() + "Book24.xlsx", m_objOpt, m_objOpt,
                 m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange,
                 m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt);
            m_objBook.Close(false, m_objOpt, m_objOpt);
            m_objExcel.Quit();
            nm += 1;
           
        }


Thanks to My self
Regards
Nilesh :thumbsup:
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900