1

I'm having a big problem porting C# code to C++ code. I'm trying to save all my datagridview info into an Excel file but it's giving me a lot of errors. These are my 2 headers and I already added the interop dll. I'm just having problems cause I've found a lot of C# info but not C++ and I'm still a student.

using namespace Microsoft::Office::Interop::Excel;

#define Excel   Microsoft::Office::Interop::Excel

// Here is the button:

Excel::Application^ xlApp =gcnew Excel::ApplicationClass();
Excel::Workbook^ xlWorkBook=xlApp->Workbooks->Add(Type::Missing); 
Excel::Worksheet xlWorkSheet =(Excel::Worksheet)xlWorkBook::Worksheets->get_Item(1); 
object misValue = System->Reflection->Missing->Value;

int i = 0;
int j = 0; 

for (i = 0; i <= DGV_ComprasUsuarios->RowCount  - 1; i++)
{
    for (j = 0; j <= DGV_ComprasUsuarios->ColumnCount  - 1; j++)
    {
        DataGridViewCell cell = DGV_ComprasUsuarios[j, i];
        xlWorkSheet.Cells[i + 1, j + 1] = cell->Value;
    }
}

xlWorkBook::SaveAs("csharp.net-informations.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook::Close(true, misValue, misValue);
xlApp::Quit();

releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);

MessageBox.Show("Excel file created , you can find the file c:\\csharp.net-informations.xls");
4

1 回答 1

0

这是我测试工作的代码。我唯一注意到的是这些值在小数点后 6、7 或 8 位后四舍五入。这应该没什么大不了的。

   System::Void yourForm::saveAttenuDataGridViewCalValues(
                 System::Windows::Forms::DataGridView^ DataGridView1)
     {
         Excel::Application^ xlApp = nullptr ;
         Excel::Workbook^ xlWorkBook = nullptr;
         Excel::Worksheet^ xlWorkSheet = nullptr;
         Object^ misValue = System::Reflection::Missing::Value;

         xlApp = gcnew Excel::ApplicationClass();
         xlWorkBook = xlApp->Workbooks->Add(misValue);
         xlWorkSheet = static_cast<Excel::Worksheet^>(xlWorkBook->Worksheets->Item[1]);
         int i = 0;
         int j = 0; 

         for (i = 0; i <= dataGridView1->RowCount  - 1; i++)
         {
             for (j = 0; j <= dataGridView1->ColumnCount  - 1; j++)
             {
                 DataGridViewCell^ cell = dataGridView1[j, i];
                 xlWorkSheet->Cells[i + 1, j + 1] = cell->Value;
             }
          }

          xlWorkBook->SaveAs("cppCli.net-informations.xls", Excel::XlFileFormat::xlWorkbookNormal, 
                             misValue, misValue, misValue, misValue,           
                             Excel::XlSaveAsAccessMode::xlExclusive, misValue, misValue, misValue, 
                             misValue, misValue);
          xlWorkBook->Close(true, misValue, misValue);
          xlApp->Quit();


           MessageBox::ShowMessage("Excel file created , you can find the file cppCli.net-
                                   informations.xls", false);
       }
于 2014-09-25T20:07:04.837 回答