DataTable dt = gr.ResultDataTable;
        try
        {
            //内存中创建一个Execl
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.CreateSheet("目录");
            //创建表头
            HSSFRow row = sheet.CreateRow(0);
            //第一列
            HSSFCell cell1 = row.CreateCell(0, HSSFCell.CELL_TYPE_STRING);
            cell1.SetCellValue("省份");
            //第二列
            cell1 = row.CreateCell(1, HSSFCell.CELL_TYPE_STRING);
            cell1.SetCellValue("");
            //第三列
            cell1 = row.CreateCell(2, HSSFCell.CELL_TYPE_STRING);
            cell1.SetCellValue("县(区)");
            //第四列
            cell1 = row.CreateCell(3, HSSFCell.CELL_TYPE_STRING);
            cell1.SetCellValue("地址");
            //第五列
            cell1 = row.CreateCell(4, HSSFCell.CELL_TYPE_STRING);
            cell1.SetCellValue("姓名");
            //第六列
            cell1 = row.CreateCell(5, HSSFCell.CELL_TYPE_STRING);
            cell1.SetCellValue("手机号码");
            //第七列
            cell1 = row.CreateCell(5, HSSFCell.CELL_TYPE_STRING);
            cell1.SetCellValue("索要时间");
            //第八列
            cell1 = row.CreateCell(5, HSSFCell.CELL_TYPE_STRING);
            cell1.SetCellValue("邮编");
            cell1 = row.CreateCell(5, HSSFCell.CELL_TYPE_STRING);
            cell1.SetCellValue("邮箱");
            //生成数据;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                row = sheet.CreateRow(i + 1);
                //第一列
                cell1 = row.CreateCell(0, HSSFCell.CELL_TYPE_STRING);
                cell1.SetCellValue(dt.Rows[i]["provice"].ToString());
                //第二列
                cell1 = row.CreateCell(1, HSSFCell.CELL_TYPE_STRING);
                cell1.SetCellValue(dt.Rows[i]["city"].ToString());
                // 第三列
                cell1 = row.CreateCell(2, HSSFCell.CELL_TYPE_STRING);
                cell1.SetCellValue(dt.Rows[i]["area"].ToString());// "ADDRESS", "CONSIGNEE", "MOBILE", "CREATE_TIME", "postalcode" 
                //第四列
                cell1 = row.CreateCell(3, HSSFCell.CELL_TYPE_STRING);
                cell1.SetCellValue(dt.Rows[i]["ADDRESS"].ToString());
                //第五列
                cell1 = row.CreateCell(4, HSSFCell.CELL_TYPE_STRING);
                cell1.SetCellValue(dt.Rows[i]["CONSIGNEE"].ToString());
                //第六列
                cell1 = row.CreateCell(6, HSSFCell.CELL_TYPE_STRING);
                cell1.SetCellValue(dt.Rows[i]["MOBILE"].ToString());
                //第七列
                cell1 = row.CreateCell(7, HSSFCell.CELL_TYPE_STRING);
                cell1.SetCellValue(dt.Rows[i]["CREATE_TIME"].ToString());
                //第八列
                cell1 = row.CreateCell(8, HSSFCell.CELL_TYPE_STRING);
                cell1.SetCellValue(dt.Rows[i]["postalcode"].ToString());
                //第九列
                cell1 = row.CreateCell(8, HSSFCell.CELL_TYPE_STRING);
                cell1.SetCellValue(dt.Rows[i]["EMAIL"].ToString());


            }
            string path =  @"E:\NPOI.xls"; //GetFullPath("Catalogue.xls");
            //保存文件
            using (FileStream fs = new FileStream(path , FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                workbook.Write(fs);

            }
posted on 2012-06-01 14:58  幻想时空  阅读(3248)  评论(0编辑  收藏  举报