none
将GV导入EXCEL问题 RRS feed

  • 问题

  •  

    public static bool ExportExcel(DataGridView gridView, string excleFileName)
            {
                if (gridView.RowCount == 0) return false;

                Application.DoEvents();

                // 创建Excel对象 
                Excel.Application xlApp = new Excel.ApplicationClass();
                if (xlApp == null)
                {
                    ClientFuncion.ShowErrorMsg("Excel无法启动");
                    return false;
                }
                // 创建Excel工作薄
                Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
                Excel.Worksheet xlSheet = (Excel.Worksheet)xlBook.Worksheets[1];

                // 设置标题
                Excel.Range range = xlSheet.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, gridView.ColumnCount]);
                range.MergeCells = true;
                xlApp.ActiveCell.FormulaR1C1 = string.Empty;
                xlApp.ActiveCell.Font.Size = 20;
                xlApp.ActiveCell.Font.Bold = true;
                xlApp.ActiveCell.HorizontalAlignment = Excel.Constants.xlCenter;

                // 列索引,行索引,总列数,总行数
                int colIndex = 0;
                int RowIndex = 0;
                int colCount = gridView.ColumnCount;
                int RowCount = gridView.BindingContext[gridView.DataSource, gridView.DataMember].Count;

                // 创建缓存数据
                object[,] objData = new object[RowCount + 1, colCount];

                // 获取列标题
                foreach (DataGridViewColumn cs in gridView.Columns)
                {
                    objData[RowIndex, colIndex++] = cs.HeaderText;
                }
                // 获取数据
                for (RowIndex = 0; RowIndex  < RowCount; RowIndex++)
                {
                    for (colIndex = 0; colIndex  < colCount; colIndex++)
                    {
                        objData[RowIndex, colIndex] = gridView[colIndex, RowIndex].FormattedValue;
                    }
                    Application.DoEvents();
                }
                // 写入Excel
                range = xlSheet.get_Range(xlApp.Cells[2, 1], xlApp.Cells[RowCount, colCount]);
                range.Value2 = objData;

                // 保存
                try
                {
                    xlBook.Saved = true;
                    xlBook.SaveCopyAs(excleFileName);
                }
                catch
                {
                    ClientFuncion.ShowErrorMsg("保存出错,请检查!");
                    return false;
                }
                finally
                {
                    xlBook = null;
                    range = null;
                    xlApp.Quit();
                    GC.Collect();
                }
                return true;
            }
        }
    想请教下我保存的文件里面就一列数据显示1字.
    可否帮我检查下哪出错了
    2008年4月12日 3:26

答案

  •  

    在VS2008和Office 2003下测试没有问题。

    另外,如果你想打印表头,应当使用下面的代码,你的代码标题虽然赋值了,但下面的代码覆盖了

     

    Code Snippet

    public static bool ExportExcel(DataGridView gridView, string excleFileName)
    {
        if (gridView.RowCount == 0) return false;

        Application.DoEvents();

        // 创建Excel对象 
        Excel.Application xlApp = new Excel.ApplicationClass();
        if (xlApp == null)
        {
            MessageBox.Show("Excel无法启动");
            return false;
        }
        // 创建Excel工作薄
        Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
        Excel.Worksheet xlSheet = (Excel.Worksheet)xlBook.Worksheets[1];

        // 列索引,行索引,总列数,总行数
        int colIndex = 0;
        int RowIndex = 0;
        int colCount = gridView.ColumnCount;
        int RowCount = gridView.BindingContext[gridView.DataSource, gridView.DataMember].Count;

        // 设置标题
        Excel.Range range = xlSheet.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, colCount]);
        range.MergeCells = true;
        xlApp.ActiveCell.FormulaR1C1 = string.Empty;
        xlApp.ActiveCell.Font.Size = 20;
        xlApp.ActiveCell.Font.Bold = true;
        xlApp.ActiveCell.HorizontalAlignment = Excel.Constants.xlCenter;
        range.Value2 = "文件标题";
      

        // 创建缓存数据
        object[,] objData = new object[RowCount + 1, colCount];

        // 获取列标题
        foreach (DataGridViewColumn cs in gridView.Columns)
        {
            objData[RowIndex, colIndex++] = cs.HeaderText;
        }

        // 获取数据
        for (RowIndex = 0; RowIndex < RowCount; RowIndex++)
        {
            for (colIndex = 0; colIndex < colCount; colIndex++)
            {
                objData[RowIndex + 1, colIndex] = gridView[colIndex, RowIndex].FormattedValue;
            }
            Application.DoEvents();
        }
        // 写入Excel
        //range = xlSheet.get_Range(xlApp.Cells[1, 1], xlApp.Cells[RowCount+1, colCount]);
        range = xlSheet.get_Range(xlApp.Cells[2, 1], xlApp.Cells[RowCount + 2, colCount]);
        range.Value2 = objData;

        // 保存
        try
        {
            xlBook.Saved = true;
            xlBook.SaveCopyAs(excleFileName);
        }
        catch
        {
            MessageBox.Show("保存出错,请检查!");
            return false;
        }
        finally
        {
            xlBook = null;
            range = null;
            xlApp.Quit();
            GC.Collect();
        }
        return true;
    }

     

     

    2008年4月12日 14:08
    版主
  • 因为你的数组中包括一行标题,以及你从Excel第二行开始填充数据导致的一些索引问题。

    黄底红字部分为你需要修改的地方:

    Code Snippet

    public static bool ExportExcel(DataGridView gridView, string excleFileName)
            {
                if (gridView.RowCount == 0) return false;

                Application.DoEvents();

                // 创建Excel对象 
                Excel.Application xlApp = new Excel.ApplicationClass();
                if (xlApp == null)
                {
                    ClientFuncion.ShowErrorMsg("Excel无法启动");
                    return false;
                }
                // 创建Excel工作薄
                Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
                Excel.Worksheet xlSheet = (Excel.Worksheet)xlBook.Worksheets[1];

                // 设置标题
                Excel.Range range = xlSheet.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, gridView.ColumnCount]);
                range.MergeCells = true;
                xlApp.ActiveCell.FormulaR1C1 = string.Empty;
                xlApp.ActiveCell.Font.Size = 20;
                xlApp.ActiveCell.Font.Bold = true;
                xlApp.ActiveCell.HorizontalAlignment = Excel.Constants.xlCenter;

                // 列索引,行索引,总列数,总行数
                int colIndex = 0;
                int RowIndex = 0;
                int colCount = gridView.ColumnCount;
                int RowCount = gridView.BindingContext[gridView.DataSource, gridView.DataMember].Count;

                // 创建缓存数据
                object[,] objData = new object[RowCount + 1, colCount];

                // 获取列标题
                foreach (DataGridViewColumn cs in gridView.Columns)
                {
                    objData[RowIndex, colIndex++] = cs.HeaderText;
                }
                // 获取数据
                for (RowIndex = 0; RowIndex  < RowCount; RowIndex++)
                {
                    for (colIndex = 0; colIndex  < colCount; colIndex++)
                    {
                        objData[RowIndex + 1, colIndex] = gridView[colIndex, RowIndex].FormattedValue;
                    }
                    Application.DoEvents();
                }
                // 写入Excel
                range = xlSheet.get_Range(xlApp.Cells[2, 1], xlApp.Cells[RowCount+2, colCount]);
                range.Value2 = objData;

                // 保存
                try
                {
                    xlBook.Saved = true;
                    xlBook.SaveCopyAs(excleFileName);
                }
                catch
                {
                    ClientFuncion.ShowErrorMsg("保存出错,请检查!");
                    return false;
                }
                finally
                {
                    xlBook = null;
                    range = null;
                    xlApp.Quit();
                    GC.Collect();
                }
                return true;
            }
        }

     

    2008年4月12日 14:58
    版主

全部回复

  •  

    在VS2008和Office 2003下测试没有问题。

    另外,如果你想打印表头,应当使用下面的代码,你的代码标题虽然赋值了,但下面的代码覆盖了

     

    Code Snippet

    public static bool ExportExcel(DataGridView gridView, string excleFileName)
    {
        if (gridView.RowCount == 0) return false;

        Application.DoEvents();

        // 创建Excel对象 
        Excel.Application xlApp = new Excel.ApplicationClass();
        if (xlApp == null)
        {
            MessageBox.Show("Excel无法启动");
            return false;
        }
        // 创建Excel工作薄
        Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
        Excel.Worksheet xlSheet = (Excel.Worksheet)xlBook.Worksheets[1];

        // 列索引,行索引,总列数,总行数
        int colIndex = 0;
        int RowIndex = 0;
        int colCount = gridView.ColumnCount;
        int RowCount = gridView.BindingContext[gridView.DataSource, gridView.DataMember].Count;

        // 设置标题
        Excel.Range range = xlSheet.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, colCount]);
        range.MergeCells = true;
        xlApp.ActiveCell.FormulaR1C1 = string.Empty;
        xlApp.ActiveCell.Font.Size = 20;
        xlApp.ActiveCell.Font.Bold = true;
        xlApp.ActiveCell.HorizontalAlignment = Excel.Constants.xlCenter;
        range.Value2 = "文件标题";
      

        // 创建缓存数据
        object[,] objData = new object[RowCount + 1, colCount];

        // 获取列标题
        foreach (DataGridViewColumn cs in gridView.Columns)
        {
            objData[RowIndex, colIndex++] = cs.HeaderText;
        }

        // 获取数据
        for (RowIndex = 0; RowIndex < RowCount; RowIndex++)
        {
            for (colIndex = 0; colIndex < colCount; colIndex++)
            {
                objData[RowIndex + 1, colIndex] = gridView[colIndex, RowIndex].FormattedValue;
            }
            Application.DoEvents();
        }
        // 写入Excel
        //range = xlSheet.get_Range(xlApp.Cells[1, 1], xlApp.Cells[RowCount+1, colCount]);
        range = xlSheet.get_Range(xlApp.Cells[2, 1], xlApp.Cells[RowCount + 2, colCount]);
        range.Value2 = objData;

        // 保存
        try
        {
            xlBook.Saved = true;
            xlBook.SaveCopyAs(excleFileName);
        }
        catch
        {
            MessageBox.Show("保存出错,请检查!");
            return false;
        }
        finally
        {
            xlBook = null;
            range = null;
            xlApp.Quit();
            GC.Collect();
        }
        return true;
    }

     

     

    2008年4月12日 14:08
    版主
  • 因为你的数组中包括一行标题,以及你从Excel第二行开始填充数据导致的一些索引问题。

    黄底红字部分为你需要修改的地方:

    Code Snippet

    public static bool ExportExcel(DataGridView gridView, string excleFileName)
            {
                if (gridView.RowCount == 0) return false;

                Application.DoEvents();

                // 创建Excel对象 
                Excel.Application xlApp = new Excel.ApplicationClass();
                if (xlApp == null)
                {
                    ClientFuncion.ShowErrorMsg("Excel无法启动");
                    return false;
                }
                // 创建Excel工作薄
                Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
                Excel.Worksheet xlSheet = (Excel.Worksheet)xlBook.Worksheets[1];

                // 设置标题
                Excel.Range range = xlSheet.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, gridView.ColumnCount]);
                range.MergeCells = true;
                xlApp.ActiveCell.FormulaR1C1 = string.Empty;
                xlApp.ActiveCell.Font.Size = 20;
                xlApp.ActiveCell.Font.Bold = true;
                xlApp.ActiveCell.HorizontalAlignment = Excel.Constants.xlCenter;

                // 列索引,行索引,总列数,总行数
                int colIndex = 0;
                int RowIndex = 0;
                int colCount = gridView.ColumnCount;
                int RowCount = gridView.BindingContext[gridView.DataSource, gridView.DataMember].Count;

                // 创建缓存数据
                object[,] objData = new object[RowCount + 1, colCount];

                // 获取列标题
                foreach (DataGridViewColumn cs in gridView.Columns)
                {
                    objData[RowIndex, colIndex++] = cs.HeaderText;
                }
                // 获取数据
                for (RowIndex = 0; RowIndex  < RowCount; RowIndex++)
                {
                    for (colIndex = 0; colIndex  < colCount; colIndex++)
                    {
                        objData[RowIndex + 1, colIndex] = gridView[colIndex, RowIndex].FormattedValue;
                    }
                    Application.DoEvents();
                }
                // 写入Excel
                range = xlSheet.get_Range(xlApp.Cells[2, 1], xlApp.Cells[RowCount+2, colCount]);
                range.Value2 = objData;

                // 保存
                try
                {
                    xlBook.Saved = true;
                    xlBook.SaveCopyAs(excleFileName);
                }
                catch
                {
                    ClientFuncion.ShowErrorMsg("保存出错,请检查!");
                    return false;
                }
                finally
                {
                    xlBook = null;
                    range = null;
                    xlApp.Quit();
                    GC.Collect();
                }
                return true;
            }
        }

     

    2008年4月12日 14:58
    版主
  • 汗……

    今天我这边网络奇慢,刚看到问题,没发现回帖,就回了,没看到孟老大已经回帖了,我的回帖提交了3遍才成功,看时间孟老大应该早就回帖了,诡异,这样帖子有些重,暂时先这样吧……

    2008年4月12日 15:10
    版主
  • 谢谢孟老大跟斑竹大人Smile

    • 已建议为答案 server126 2009年12月8日 3:38
    2008年4月12日 15:27