解决方案 »

  1.   

    导出的时候,新建excel。
    循环Datatable的每一行。新建一个r单元格(range),对不同列的数据进行相应的赋值操作
      

  2.   

    rowIndex = startRow + head.Rows.Count;
                    foreach (DataRow row in data.Rows)
                    {
                        for (int j = 0; j < columnNames.Length; j++)
                        {
                            if (data.Columns[columnNames[j]] != null)
                            {
                                sheet.Cells[rowIndex, j + 1] = row[columnNames[j]];
                            }
                        }
                        rowIndex++;
                    }
    我这不多就是你说的那样导的,图片会出错,类型异常,你导过吗,分享一下代码吧
      

  3.   


                     rowIndex = startRow + head.Rows.Count;
                     foreach (DataRow row in data.Rows)
                     {
                         for (int j = 0; j < columnNames.Length; j++)
                         {
                             if (data.Columns[columnNames[j]] != null)
                             {
                                 sheet.Cells[rowIndex, j + 1] = row[columnNames[j]];
                             }
                         }
                         rowIndex++;
                     }
     我这不多就是你说的那样导的,图片会出错,类型异常,你导过吗,分享一下代码吧 
      

  4.   

    这是我昨天做的。你看一下咯。
    System.Data.DataTable dataTable = dataSet.Tables[0];
                int rowNumber = dataTable.Rows.Count;
                int columnNumber = dataTable.Columns.Count;
                if (rowNumber == 0)
                {
                    MessageBox.Show("没有任何数据可以导入到Excel文件!");
                    return false;
                }            //建立Excel对象
                Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
                Workbook xlbook = excel.Application.Workbooks.Add(true);
                Worksheet xlsheet = (Worksheet)xlbook.Worksheets[1];
                xlsheet.Name = "库存报表";            //表头样式
                excel.ActiveSheet.Rows[2].Font.Name = "隶书";
                excel.ActiveSheet.Rows[2].Font.Bold = true;
                excel.ActiveSheet.Rows[2].Font.Size = 20;
                //excel.ActiveSheet.Rows[2].Font.UnderLine = true;            //列名样式
                excel.ActiveSheet.Rows[7].Font.Name = "隶书";
                excel.ActiveSheet.Rows[7].Font.Bold = true;
                //excel.ActiveSheet.Rows[7].Font.UnderLine = true;            int colIndex = 0;
                int RowIndex = 7;            excel.Cells[2, 7] = "深圳市********有限公司";
                excel.Cells[4, 1] = "客户名称:";
                excel.Cells[4, 3] = this.UCSelectCoagent.Text;
                excel.Cells[4, 8] = "所在仓库:";
                excel.Cells[4, 10] = this.CMBWareAddress.Text;            excel.Cells[5, 1] = "库存截止时间:";
                excel.Cells[5, 3] = DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day;
                excel.Cells[5, 8] = "报表生成时间:";
                excel.Cells[5, 10] = DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day;
                //开始写入每列的标题 
                foreach (DataColumn dc in dataTable.Columns)
                {
                    colIndex++;
                    excel.Cells[RowIndex, colIndex] = dc.Caption;
                }
                //填充数据
                for (int c = 0; c < rowNumber; c++)
                {
                    for (int j = 0; j < columnNumber; j++)
                    {
                        excel.Cells[RowIndex + 1, j + 1] = dataTable.Rows[c].ItemArray[j];
                    }
                    RowIndex++;
                }
     
                Microsoft.Office.Interop.Excel.Range range5_7 = xlsheet.Range[xlsheet.Cells[7, 1], xlsheet.Cells[RowIndex, columnNumber]];
                //内外边框
                range5_7.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
                range5_7.Borders.Weight = 2;            string sampleFile = string.Format("库存报表{0}.xlsx", DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day);
                SaveFileDialog sd = new SaveFileDialog();
                sd.FileName = sampleFile;
                sd.Filter = "文档类型(*.xls;*.xlsx)|*.xls;*.xlsx|所有文件(*.xls;*.xlsx)|*.xls;*.xlsx";
                if (DialogResult.OK == sd.ShowDialog())
                {
                    string FileNames = sd.FileName.ToString();
                    xlbook.SaveCopyAs(FileNames); 
                }
      

  5.   

    我的这个方法你可以直接复制过去用。在一个按钮的单击事件调用我那个方法就行。然后看效果去修改成你想要的请教7楼,你确定这个可以导出Datatable中的图片列(Byte[]),我这边报错的。我原来的写法是这样的 sheet.Cells[rowIndex, j + 1] = row[columnNames[j]];  跟你的差不多