Import Array list to excel format with out using for loop in C#

跟風遠走 提交于 2021-01-29 14:50:39

问题


E.g. I have huge number of data in my list, I done it using for loop. But taking more time to import the data into excel. I tried below code with out using for loop. But i'm getting below error in range.set_Value.

Error:An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll Additional information: Exception from HRESULT: 0x800A03EC

    List<myobj> NGDetailsList = new List<myobj>();
    NGDetailsList.Add(new myobj { name = "AAA", designation = "test" });
    NGDetailsList.Add(new myobj { name = "BBB", designation = "test1" });
        Microsoft.Office.Interop.Excel.Application PcInfoFile;
    Microsoft.Office.Interop.Excel.Workbook workbook;
    Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet; 

    PcInfoFile = new Microsoft.Office.Interop.Excel.Application();
    PcInfoFile.Visible = false;

    PcInfoFile.Visible = true;
    workbook = PcInfoFile.Workbooks.Open(@"C:\Users\AAAA\Desktop\New Microsoft Excel Worksheet.xlsx");

    xlWorkSheet = workbook.Worksheets.get_Item(1);
    // Get dimensions of the 2-d array
    int rowCount = 2;
    int columnCount = 2;

    // Get an Excel Range of the same dimensions
   Microsoft.Office.Interop.Excel.Range range =    (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1];
   range = range.get_Resize(rowCount, columnCount);

    // Assign the 2-d array to the Excel Range
    range.set_Value(Microsoft.Office.Interop.Excel.XlRangeValueDataType.xlRangeValueDefault, NGDetailsList);

回答1:


Sample code below Worked For me:

public class ExportImportExcel
    {
        #region Constructor
        public ExportImportExcel()
        {

        }
        #endregion Constructor

        #region Methods
        public static void ExportToExel(DataGridView DGV, System.Data.DataTable Dt)
        {
            try
            {
                //Install-Package Microsoft.Office.Interop.Excel
                Microsoft.Office.Interop.Excel.Application exeleapp = new Microsoft.Office.Interop.Excel.Application();
                var exelbook = exeleapp.Workbooks.Add(XlSheetType.xlWorksheet);
                var exelworksheet = (Worksheet)(exelbook.Worksheets[1]);
                exelworksheet.DisplayRightToLeft = true;

                int clmncnt = DGV.Columns.Count;
                Range[] rng = new Range[clmncnt];

                for (int x = 0; x < clmncnt; x++)
                {
                    string celladress = Convert.ToString(Convert.ToChar(Convert.ToByte(x + 65))) + "1";
                    rng[x] = exelworksheet.get_Range(celladress, celladress);
                    rng[x].Value2 = DGV.Columns[x].HeaderText;
                }

                int j = 2;
                foreach (DataRow r in Dt.Rows)
                {
                    for (int k = 0; k < clmncnt; k++)
                    {
                        string celladress = Convert.ToString(Convert.ToChar(Convert.ToByte(k + 65))) + j.ToString();
                        rng[k] = exelworksheet.get_Range(celladress, celladress);
                        rng[k].Value2 = r[k].ToString();
                    }
                    j++;
                }
                exeleapp.Visible = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);              
            }
        }

        #endregion Methods
    }

How to call class:

private void ExportExcel()
        {          
            ExportImportExcel.ExportToExel(DgvInfo, patientDataViewDS.PatientDataView);           
        }

  private void Btn_ExportEXEL_Click(object sender, EventArgs e)
        {            
            var thread1 = new Thread(ExportExcel);
            thread1.Start();                      
        }

I hope this sample help to you.




回答2:


Found the answers and now it is working fine. Solution : Used 2D array instead of list.

       object[,] values = new object[5,4]; 

       // row, column on worksheet
       values[0, 0] = 11;
       values[0, 1] = 12; 
       values[0, 2] = 13; 

       values[1, 0] = 21;
       values[1, 1] = 22;
       values[1, 2] = 23;

       values[2, 0] = 31;
       values[2, 1] = 32;
       values[2, 2] = 32;

       values[3, 0] = 41;
       values[3, 1] = 42;
       values[3, 2] = 42;

       values[4, 0] = 51;  
       values[4, 1] = 52; 
       values[4,2] = 52;

       range.set_Value(Microsoft.Office.Interop.Excel.XlRangeValueDataType.xlRangeValueDefault, values);


来源:https://stackoverflow.com/questions/60409467/import-array-list-to-excel-format-with-out-using-for-loop-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!