如何将dataset导入到excel表中的多个sheet中

 //一个dataset到一个excel文件
        public void doExport(DataSet ds,string strExcelFileName)
        {
            String[] tableNames = new String[] { 多个表名 };
            
            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            try
            {
                excel.Visible = false;
                //设置禁止弹出保存和覆盖的询问提示框
                excel.DisplayAlerts = false;
                excel.AlertBeforeOverwriting = false;

                //增加一个工作簿
                Workbook book = excel.Workbooks.Add(true);
                //添加工作表
                Worksheet sheets = (Microsoft.Office.Interop.Excel.Worksheet)
                    book.Worksheets.Add(Missing.Value, Missing.Value, 9, Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet);
               
                for (int i = 0 ; i < 表的数量; i++)
                {
                    System.Data.DataTable table = ds.Tables[i];
                    //获取一个工作表
                    Worksheet sheet = book.Worksheets[i + 1] as Worksheet;
                    int rowIndex = 1;
                    int colIndex = 0;

                    foreach (DataColumn col in table.Columns)
                    {
                        colIndex++;
                        sheet.Cells[1, colIndex] = col.ColumnName;
                    }
                    foreach (DataRow row in table.Rows)
                    {
                        rowIndex++;
                        colIndex = 0;
                        foreach (DataColumn col in table.Columns)
                        {
                            colIndex++;
                            //sheet.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
                            //在这里要在数字前加前单引号
                            String typeName = row[col.ColumnName].GetType().ToString();
                            sheet.Cells[rowIndex, colIndex] = typeCheckAdd(row[col.ColumnName].ToString(), typeName);
                        }
                    }
                    sheet.Name = tableNames[i];
                }

                //book.Save();
                book.SaveAs(strExcelFileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                   Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                //excel.Save(strExcelFileName);

            }
            catch (Exception err)
            {
                MessageBox.Show("导入Excel出错!错误原因:" + err.Message, "错误提示",MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                excel.Quit();
                excel = null;
                GC.Collect();
            }
        }
        //若是大数需加前导引号变成字符串
        public String typeCheckAdd(String cellContent,String strType)
        {
            String cellContentAdd;
            switch (strType)
            {
                case "System.String":
                    cellContentAdd = "'" + cellContent;
                    break;
                default:
                    cellContentAdd = cellContent;
                    break;

            }
            return cellContentAdd;
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值