SoFunction
Updated on 2025-03-07

Summary of several methods of communication processing between C# forms

I have encountered problems in exporting Excel recently when doing projects:
Look at the code:

Copy the codeThe code is as follows:

/// <summary>
/// Methods to generate Excel
        /// </summary>
        /// <param name="ds">DataSet</param>
/// <param name="url">The relative address of the server that exists in Excel</param>
        /// <returns></returns>
        private bool ExportExcel(DataSet ds, string path)
        {
//Create the title line
            HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = ("Registration status");
            HSSFRow rowtitle = (0);


//Set column width
            (0, 30 * 256);
            (1, 30 * 256);
            (2, 30 * 256);
            (3, 30 * 256);
            (4, 30 * 256);
            (5, 30 * 256);
            (6, 30 * 256);
            (7, 30 * 256);
            (8, 30 * 256);
            (9, 30 * 256);

 


//Create column
(0,).SetCellValue("name");
(1, ).SetCellValue("Qualification Certificate Number");
(2, ).SetCellValue("Professional Qualification Level");
(3, ).SetCellValue("Gender");
(4, ).SetCellValue("ID number");
(5, ).SetCellValue("Professional Information Identification Card Number");
(6, ).SetCellValue("original organization name");
(7, ).SetCellValue("original mechanism number");
(8, ).SetCellValue("Change the organization name");
(9, ).SetCellValue("Change the organization number");


//DataSet is a collection of DataTale. If only 1 table is populated, the ID of this table is 0.
            DataTable dt = [0];
            int i = 1;
            foreach (DataRow row in )
            {
                HSSFRow newrow = (i);
                (0,).SetCellValue((row["R_XM"]));
                (1, ).SetCellValue((row["R_newzzbh"]));
                string jibie=;
                if (row["R_jb"].ToString()=="1")
                {
jibie = "level one";
                }
                else if (row["R_jb"].ToString() == "2")
                {
jibie = "level 2";
                }
                else if (row["R_jb"].ToString() == "3")
                {
jibie = "level three";
                }
                (2,).SetCellValue(jibie);
                (3,).SetCellValue((row["R_XB"]));
                (4,).SetCellValue((row["user_id"]));
                (5,).SetCellValue((row["R_KH"]));
                (6,).SetCellValue((row["yjgmc"]));
                (7,).SetCellValue((row["yjgbh"]));
                (8,).SetCellValue((row["bjgmc"]));
                (9,).SetCellValue((row["bjgbh"]));
                i++;
            }
            try
            {
                using (Stream stream = (path, , ))
                {
                    (stream);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
                throw;
            }

        }


Export method:
Copy the codeThe code is as follows:

/// <summary>
/// Export Excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void linkExport_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            ds = ();
            Random rd = new Random();
            int rd1= (111111,999999);
            string path = ("~\\anxieExecl\\") + ("yyyyMMddhhmmss")+ () + ".xls";
            if (!(("~\\anxieExecl\\")))
            {
                (("~\\anxieExecl\\"));
            }
            bool status = ExportExcel(ds,path);
            string Redirectpath = "~\\anxieExecl\\" + (("\\") + 1);
            if (status)
            {
                (Redirectpath);
                (path);
            }
            else
            {
(GetType(), "alert", "alert('Creating Excel failed!')", true);
            }
        }