比如一个DataSet中有多个DataTable,现在要将这个DataSet数据集中的表导出一个Excel的Sheet文件中。
表1 :
ID 姓名 年龄
1  x1  15
2  x2  16
3  x3  17
表2 :
ID 种族 国籍
1  汉族 中国
2  回族 美国
导出到Excel的一个Sheet中   Sheet1
可是 出现了错列
结果如下:
1 x1 15
2 x2 16
3 x3 17
1       汉族 中国
2       回族 美国
请问是怎么回事?

解决方案 »

  1.   

    补充 我的代码如下:
    protected void export(DataSet[] datasets)
        {
            //XlsDocument对象
            AppLibrary.WriteExcel.XlsDocument doc = new AppLibrary.WriteExcel.XlsDocument();
            //保存的文件名
            doc.FileName = "Report.xls";
            //工作簿
            AppLibrary.WriteExcel.Worksheet sheet;
            //单元格
            AppLibrary.WriteExcel.Cells cells;
            foreach (DataSet ds in datasets)
            {
                if (ds != null)
                {
                    string sheetName = ds.DataSetName.ToString();
                    sheet = doc.Workbook.Worksheets.Add(sheetName);                cells = sheet.Cells;
                    foreach (DataTable dt in ds.Tables)
                    {
                        if (dt.Rows.Count > 0)
                        {
                            for (int rowIndex = 1; rowIndex <= dt.Rows.Count; rowIndex++)
                            {
                                for (int colIndex = 1; colIndex <= dt.Columns.Count; colIndex++)
                                {
                                    cells.Add(rowIndex, colIndex, dt.Rows[rowIndex-1][colIndex-1].ToString());                            }
                            }
                        }
                    }
                }
            }
            doc.Send();
            Response.Flush();
            Response.End();
        }引用了一个类库文件AppLibrary
      

  2.   

    到第一个datatable,rowIndex, colIndex就又为0
      

  3.   

    不明白你说的意思。拜托仔细看结果,RowIndex是一直在增的,ColIndex第一个是正确的,可是后两个就错列了!
      

  4.   

    问题已解决,向DataSet中添加数据表的时候要添加表名,
    否则它会将多个表合成一个表。
      

  5.   

    http://www.codeproject.com/KB/office/export.aspx