简体   繁体   中英

Getting Specific Data in DataSet Column and export in excel

i have 5 column in my dataset. name, age,sex, address and contact number then i have 3 datarows in dataset

i need only name, address and contact number during export in excel.. how can i do that? im using the using Excel = Microsoft.Office.Interop.Excel; i only get the 1st row.. not all rows in data set

this is my code for getting the values

Microsoft.Office.Interop.Excel.Application ExcelAp = new Microsoft.Office.Interop.Excel.Application();
ExcelAp.Application.Workbooks.Add(Type.Missing);
ExcelAp.Columns.ColumnWidth = 25;

int i =0;
foreach( DataRow datarow in DsNow.Tables[0].Rows)
{
      for(i = 0; DsNow.Tables[0].Rows.Count - 1; i ++)
            {
                         ExcelAp.Cells[i + 6, 1] = datarow["name"].ToString();
                         ExcelAp.Cells[i + 6, 2] =datarow["address"].ToString();
                         ExcelAp.Cells[i + 6, 3] =datarow["contactnumber"].ToString(); 
            }
} 

please help

WHy use both FOREACH and FOR loop when it can be done with one loop only. CHeck the below code with little modification in your code:

int i =0;
      for(i = 0; DsNow.Tables[0].Rows.Count - 1; i ++)
            {
                  ExcelAp.Cells[i + 6, 1] = DsNow.Tables[0].Rows[i]["name"].ToString();
                  ExcelAp.Cells[i + 6, 2] = DsNow.Tables[0].Rows[i]["address"].ToString();
                  ExcelAp.Cells[i + 6, 3] = DsNow.Tables[0].Rows[i]["contactnumber"].ToString(); 
            }
         Public void Excel()
        {
         Microsoft.Office.Interop.Excel.Application app;
         Workbook workbook = null;
         Worksheet worksheet = null;
         Range range = null;   
         int i,j,k=1;
         app = new Microsoft.Office.Interop.Excel.Application();
         app.Visible = false;
         workbook = app.Workbooks.Add(1);
         worksheet = (Worksheet)workbook.Sheets[1];
         worksheet.Name = "Sheet1";  
         worksheet.Cells.Locked = false;

        //Column1
        j = 1;
        range = worksheet.Rows.get_Range("A" + j.ToString(), "A" + j.ToString());
        range.Font.Size = 11;
        ((Range)worksheet.Cells[j, 1]).EntireColumn.ColumnWidth = 15;
        range.Font.Bold = true;
        range.MergeCells = true;
        range.HorizontalAlignment = Constants.xlCenter;
        //Content
        worksheet.Cells[j, 1] = "Name";

        //Column2
        range = worksheet.Rows.get_Range("B" + j.ToString(), "B" + j.ToString());
        range.Font.Size = 11;
        ((Range)worksheet.Cells[j, 2]).EntireColumn.ColumnWidth = 15;
        range.Font.Bold = true;
        range.MergeCells = true;
        range.HorizontalAlignment = Constants.xlCenter;
        //Content
        worksheet.Cells[j, 2] = "Address";


        //Column3
        range = worksheet.Rows.get_Range("C" + j.ToString(), "C" + j.ToString());
        range.Font.Size = 11;
        ((Range)worksheet.Cells[j, 3]).EntireColumn.ColumnWidth = 15;
        range.Font.Bold = true;
        range.MergeCells = true;
        range.HorizontalAlignment = Constants.xlCenter;
        //Content
        worksheet.Cells[j, 3] = "Contact Number";

       //Loop
        DataTable dt = new DataTable();

        for (i = 0; i < dt.Rows.Count; i++)
       {
            //Column1
            range = worksheet.Rows.get_Range("A" + k.ToString(), "A" + k.ToString());
            worksheet.Cells[k, 1] = dt.Rows[i]["NAME"].ToString();

            //Column2
            range = worksheet.Rows.get_Range("B" + k.ToString(), "B" + k.ToString());
            worksheet.Cells[k, 2] = dt.Rows[i]["ADDRESS"].ToString();

            //Column3
            range = worksheet.Rows.get_Range("C" + k.ToString(), "C" + k.ToString());
            worksheet.Cells[k, 3] = dt.Rows[i]["CONTACT_NUMBER"].ToString();

            k = k +1;
       }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM