-2

I have an application linked to a database. I recover the data and display it in a gridView. Now I'm trying to make my button extract my xls or csv file.

My datagrid : My datagrid

my button code extract my button

So, I would like change the

xlWorkSheet.Cells[1, 1] = "Id Action";" //by my database or my datagridView. 

to

xlWorkSheet.Cells["all"] =  ADOAction.dataGridAction(var1, var2);

To summarize if it was unclear :

  • a basic application for extract data on database
  • I have one view which shows the datagridview
  • I have dataGrid function for display in datagridview with database values
  • I have a button on view which extract the file.xls but the data is not on my database, it's just a hard data...

So if you have an idea of my problem or even links to me, it will make me happy.

Bye, good day !

t0mm13b
  • 34,087
  • 8
  • 78
  • 110
  • 3
    Please stop posting code as *images*, that is one good way to invoke the wrath of the SO community. Read the [FAQ]. Show us what you have tried, what you debugged. – t0mm13b Nov 06 '17 at 10:55
  • 1
    I downvoted because of http://idownvotedbecau.se/imageofcode, you can find an answer here with cell formatting: https://stackoverflow.com/questions/39210329/export-the-datagridview-to-excel-with-all-the-cells-format - we have no idea what you meant by *button on view which extract the file.xls* **but the data is not on my database, it's just a hard data...*** – Jeremy Thompson Nov 06 '17 at 12:19
  • Sorry for image but, the code is broken... I followed this simple explanation but I want to use data in my database : http://csharp.net-informations.com/excel/csharp-create-excel.htm – Lucas Monrolin Nov 06 '17 at 13:58

2 Answers2

0

try this to export data into excel in windows form data grid view

private void btnExportExcel_Click(object sender, EventArgs e)
{
    YourDataGridID.SelectAll();
    DataObject dataObj = YourDataGridID.GetClipboardContent();
    if (dataObj != null)
        Clipboard.SetDataObject(dataObj);
    GenerateExcel();
}

private static void GenerateExcel()
{
    Microsoft.Office.Interop.Excel.Application xlexcel;
    Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
    Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
    object misValue = System.Reflection.Missing.Value;
    xlexcel = new Microsoft.Office.Interop.Excel.Application();
    xlexcel.Visible = true;
    xlWorkBook = xlexcel.Workbooks.Add(misValue);
    xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
    Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1];
    CR.Select();
    xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
}
  • I have 2 errors, first : GetClipBoardContent() (using is missing, i think) And second error is GenerateExcel is inaccessible due to its level of protection... – Lucas Monrolin Nov 06 '17 at 13:52
  • Now I have just 1 errors : GetClipBoardContent()... but i don't konw ... i try this : https://stackoverflow.com/questions/1478342/datagridview-getclipboardcontent-equivalent-on-datagrid But it's not okay. But your system is good, create the excel and open with data is very good... just this error for recover datas in the datagrid :/ – Lucas Monrolin Nov 07 '17 at 08:57
0

So, I searched and I found that is not bad :

you have to have a file.xlsx already created !!

I download the ClosedXML on my Visual studio 2015 with NuGet : https://github.com/ClosedXML/ClosedXML/blob/develop/README.md

Then I typed the following code :

        XLWorkbook wb = new XLWorkbook("c:\\path...\\file.xlsx"); 
        DataTable dt = functiondatabase();
        wb.Worksheets.Add(dt, "NameOfSheets");
        wb.Save();

And this using :

using ClosedXML.Excel;

Now you have data in your file but warning ! If your file.xlsx is already filled you have a error !

So thank for your response and good day all ! If you have questions send me on private message.

Bye :)