如何将datagridview标题文本导出到Excel第1行第5行 [英] How do I export datagridview header text to excel column 1 row 5

查看:103
本文介绍了如何将datagridview标题文本导出到Excel第1行第5行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法将datagridview1导出到excel工作表。问题是我希望标题文本从第5行开始而不是第1行。



我尝试过:



I managed to export datagridview1 to excel worksheet. The problem is i want the header text to start in row 5 and not row 1.

What I have tried:

<blockquote class="quote"><div class="op">Quote:</div> int cr = 1;
            int cc = 1;
           
            //Loop through each row and read value from each column. 
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {

                for (int j = 0; j < dataGridView1.Columns.Count; j++)
                {
                    // Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check. 
                    if (cr == 1)
                    {
                        xlWorkSheet.Cells[cr, cc] = dataGridView1.Columns[j].HeaderText;
                    }

                    cc++;
                }
                cc = 1;
                cr++;


            } 
            //get data:
            //you can erase this if you dont need data from rows:
            for (int i = 0; i <= dataGridView1.RowCount - 1; i++)
            {
                for (int j = 0; j <dataGridView1.Columns.Count; j++)
                {
                    DataGridViewCell cell = dataGridView1[j, i];
                    xlWorkSheet.Cells[i + 2, j + 1] = cell.Value;
                    
                }
            }</blockquote>

推荐答案

Excel.Application xlApp ;
            Excel.Workbook xlWorkBook ;
            Excel.Worksheet xlWorkSheet ;
            object misValue = System.Reflection.Missing.Value;
            
            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            
            //int i = 1;
            //int j = 1;
            
            string datestr = DateTime.Now.ToShortDateString();
            //string colName = dataGridView1.Columns[j].HeaderText;
            string filename = "Quote_" + txtName.Text.Replace(" ", "") + "_" + datestr + ".xls";

            int cr = 5;
            int cc = 1;
            
            //Loop through each row and read value from each column. 
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {

                for (int j = 0; j < dataGridView1.Columns.Count; j++)
                {
                    // Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check. 
                    if (cr == 5)
                    {
                        xlWorkSheet.Cells[cr, cc] = dataGridView1.Columns[j].HeaderText;
                    }

                    cc++;
                }
                cc = 1;
                cr++;


            } 
            //get data:
            //you can erase this if you dont need data from rows:
            for (int i = 0; i <= dataGridView1.RowCount - 1; i++)
            {
                for (int j = 0; j <dataGridView1.Columns.Count; j++)
                {
                    DataGridViewCell cell = dataGridView1[j, i];
                    xlWorkSheet.Cells[i + 2, j + 1] = cell.Value;
                    xlWorkSheet.Columns.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
                    
                }
            }
  

            xlWorkBook.SaveAs(filename, 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:\\quote.xls");
        }


这篇关于如何将datagridview标题文本导出到Excel第1行第5行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆