C〓〓のフォーム間の通信の処理のいくつかの方法は総括します。


最近プロジェクトをしてExcelをエクスポートする問題に遭遇しました。まとめてみます。コードを見てください。

/// <summary>
        /// Excel
        /// </summary>
        /// <param name="ds">DataSet</param>
        /// <param name="url">Excel </param>
        /// <returns></returns>
        private bool ExportExcel(DataSet ds, string path)
        {
            //
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.CreateSheet(" ");
            HSSFRow rowtitle = sheet.CreateRow(0);


            //
            sheet.SetColumnWidth(0, 30 * 256);
            sheet.SetColumnWidth(1, 30 * 256);
            sheet.SetColumnWidth(2, 30 * 256);
            sheet.SetColumnWidth(3, 30 * 256);
            sheet.SetColumnWidth(4, 30 * 256);
            sheet.SetColumnWidth(5, 30 * 256);
            sheet.SetColumnWidth(6, 30 * 256);
            sheet.SetColumnWidth(7, 30 * 256);
            sheet.SetColumnWidth(8, 30 * 256);
            sheet.SetColumnWidth(9, 30 * 256);

 


            //
            rowtitle.CreateCell(0,HSSFCellType.STRING).SetCellValue(" ");
            rowtitle.CreateCell(1, HSSFCellType.STRING).SetCellValue(" ");
            rowtitle.CreateCell(2, HSSFCellType.STRING).SetCellValue(" ");
            rowtitle.CreateCell(3, HSSFCellType.STRING).SetCellValue(" ");
            rowtitle.CreateCell(4, HSSFCellType.STRING).SetCellValue(" ");
            rowtitle.CreateCell(5, HSSFCellType.STRING).SetCellValue(" ");
            rowtitle.CreateCell(6, HSSFCellType.STRING).SetCellValue(" ");
            rowtitle.CreateCell(7, HSSFCellType.STRING).SetCellValue(" ");
            rowtitle.CreateCell(8, HSSFCellType.STRING).SetCellValue(" ");
            rowtitle.CreateCell(9, HSSFCellType.STRING).SetCellValue(" ");


            //DataSet DataTale , 1 , ID 0
            DataTable dt = ds.Tables[0];
            int i = 1;
            foreach (DataRow row in dt.Rows)
            {
                HSSFRow newrow = sheet.CreateRow(i);
                newrow.CreateCell(0,HSSFCellType.STRING).SetCellValue(Convert.ToString(row["R_XM"]));
                newrow.CreateCell(1, HSSFCellType.STRING).SetCellValue(Convert.ToString(row["R_newzzbh"]));
                string jibie=string.Empty;
                if (row["R_jb"].ToString()=="1")
                {
                    jibie = " ";
                }
                else if (row["R_jb"].ToString() == "2")
                {
                    jibie = " ";
                }
                else if (row["R_jb"].ToString() == "3")
                {
                    jibie = " ";
                }
                newrow.CreateCell(2,HSSFCellType.STRING).SetCellValue(jibie);
                newrow.CreateCell(3,HSSFCellType.STRING).SetCellValue(Convert.ToString(row["R_XB"]));
                newrow.CreateCell(4,HSSFCellType.STRING).SetCellValue(Convert.ToString(row["user_id"]));
                newrow.CreateCell(5,HSSFCellType.STRING).SetCellValue(Convert.ToString(row["R_KH"]));
                newrow.CreateCell(6,HSSFCellType.STRING).SetCellValue(Convert.ToString(row["yjgmc"]));
                newrow.CreateCell(7,HSSFCellType.STRING).SetCellValue(Convert.ToString(row["yjgbh"]));
                newrow.CreateCell(8,HSSFCellType.STRING).SetCellValue(Convert.ToString(row["bjgmc"]));
                newrow.CreateCell(9,HSSFCellType.STRING).SetCellValue(Convert.ToString(row["bjgbh"]));
                i++;
            }
            try
            {
                using (Stream stream = File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                {
                    workbook.Write(stream);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
                throw;
            }

        }

エクスポート方法:

/// <summary>
        /// Excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void linkExport_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            ds = apryManager.Export();
            Random rd = new Random();
            int rd1= rd.Next(111111,999999);
            string path = this.Server.MapPath("~\\anxieExecl\\") + DateTime.Now.ToString("yyyyMMddhhmmss")+ rd1.ToString() + ".xls";
            if (!Directory.Exists(this.Server.MapPath("~\\anxieExecl\\")))
            {
                Directory.CreateDirectory(this.Server.MapPath("~\\anxieExecl\\"));
            }
            bool status = ExportExcel(ds,path);
            string Redirectpath = "~\\anxieExecl\\" + path.Substring(path.LastIndexOf("\\") + 1);
            if (status)
            {
                Response.Redirect(Redirectpath);
                File.Delete(path);
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "alert", "alert(' Excel !')", true);
            }
        }