Click here to Skip to main content
15,890,995 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi!

i want with a button to send the table to an excel file??

how i can do this??

i see many things but i am confused!!!
Posted

If you are talking about WinForms,the easiest way is OLEDB.The harder is Excel interop.If you are to modify visuals of excel sheets, excel interop is preferable.
 
Share this answer
 
Comments
lalaou 4-Apr-12 18:18pm    
I try excel interop but i cant do it!
I can help if you can write where/how excel interop failed?
 
Share this answer
 
Comments
dieforwhat 4-Apr-12 18:25pm    
But if I were you,I would absolutely use OLEDB.
lalaou 4-Apr-12 19:41pm    
ok! i try to find a solution but i cant!!
can you give me an example of how to import a table to excel with a button?
(i am totally new in visual studio and c#)
You use Excel Interopt, which is not too bad. Here is an example of how to create an Excel file:
C#
private static void Excel(string fileName, List<IDirectoryInventoryDataCollector> list)
{
    try
    {
        var xlApp = new Excel.Application();
        var xlWorkBook = xlApp.Workbooks.Add();
        var xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
        ExcelTitleRow(list[0], 1, xlWorkSheet);

        int row = 2;
        foreach (var item in list)
        {
            ExcelFillRow(item, row++, xlWorkSheet);
        }

        for (int i = 1; i < list[0].MaxLevel - 1; i++)
        {
            ((Range)xlWorkSheet.Columns[i]).ColumnWidth = 2;
        }
        ((Range)xlWorkSheet.Columns[list[0].MaxLevel - 1]).ColumnWidth = 30;
        ((Range)xlWorkSheet.Rows[1]).WrapText = true;
        ((Range)xlWorkSheet.Rows[1]).HorizontalAlignment = HorizontalAlignment.Center;
        ((Range)xlWorkSheet.Cells[1, 1]).WrapText = false;

        xlWorkBook.SaveAs(fileName);
        xlWorkBook.Close();
        xlApp.Quit();
    }
    catch (AccessViolationException)
    {
        System.Windows.Forms.MessageBox.Show(
             "Have encountered access violation. This could be issue with Excel 2000 if that is only version installed on computer",
             "Access Violation");
    }
    catch (Exception)
    {
        System.Windows.Forms.MessageBox.Show("Unknown error",
             "Unknown error");
    }
}

private static void ExcelFillRow(IDirectoryInventoryDataCollector item, int row, Excel.Worksheet sheet)
{
    sheet.Cells[row, item.Level] = item.Name;
    int column = item.MaxLevel;
    foreach (var property in item.GetProperties())
    {
        sheet.Cells[row, column++] = property;
    }
}

private static void ExcelTitleRow(IDirectoryInventoryDataCollector item, int row, Excel.Worksheet sheet)
{
    sheet.Cells[row, 1] = "Name";
    int column = item.MaxLevel;
    foreach (var property in item.GetPropertyNames())
    {
        sheet.Cells[row, column++] = property;
    }
}
 
Share this answer
 
this function is in windows forms
Add reference  of Microsoft.Office.Interop.Excel to your application(11 version)
//include this in namespaces

using Excel = Microsoft.Office.Interop.Excel;

//dataset contains the data you want to export
//drivename means the localdiskdrive name
//foldername means in which folder you want to save the excel file
//filename is the name of excel file


public void ExportToExcel(DataSet ds, string drivename, string foldername, string filename)
        {
            if (ds.Tables[0].Rows.Count != 0)
            {
                XmlDataDocument xmlDoc = new System.Xml.XmlDataDocument(ds);
                XslCompiledTransform xslTran = new System.Xml.Xsl.XslCompiledTransform();

                //Create XmlTextWriter for the FileSteam
                //FileStream fs = new System.IO.FileStream("test.xls",FileMode.OpenOrCreate);
                DirectoryInfo di = new DirectoryInfo(drivename + ":\\" + foldername);
                if (di.Exists == false)
                    di.Create();

                FileStream fs = new System.IO.FileStream(drivename + ":\\" + foldername + "\\" + filename + ".xls", FileMode.OpenOrCreate);

                XmlTextWriter xtw = new System.Xml.XmlTextWriter(fs, System.Text.Encoding.Unicode);

                //Add processing instructions to the beginning of the XML file,X
                // one of which indicates a style sheet.
                xtw.WriteProcessingInstruction("xml", "version='1.0'");
                string strXSLFilename;
                strXSLFilename = "test.xsl";
                xtw.WriteProcessingInstruction("xml-stylesheet", "type='text/xls' href='" + strXSLFilename + "'");

                //Write the XML from the dataset to the file
                ds.WriteXml(xtw);
                xtw.Close();
                MessageBox.Show("Successfully exported to Excel sheet please verify" + drivename + ":\\" + foldername + "\\", "Export");

            }
            else
            {
                MessageBox.Show("No Records found to Export.", "Warning");
            }


        }
 
Share this answer
 
v2

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



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