Summary of several methods of communication between C forms
- 2020-05-24 05:58:52
- OfStack
Recently, I encountered the problem of exporting Excel in my project.
Look at the code:
/// <summary>
/// generate Excel The method of
/// </summary>
/// <param name="ds">DataSet</param>
/// <param name="url">Excel The relative address of the server exists </param>
/// <returns></returns>
private bool ExportExcel(DataSet ds, string path)
{
// Create a header row
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.CreateSheet(" Enrollment situation ");
HSSFRow rowtitle = sheet.CreateRow(0);
// Set the column width
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);
// Create the columns
rowtitle.CreateCell(0,HSSFCellType.STRING).SetCellValue(" The name ");
rowtitle.CreateCell(1, HSSFCellType.STRING).SetCellValue(" Qualification certificate no. ");
rowtitle.CreateCell(2, HSSFCellType.STRING).SetCellValue(" Vocational qualification level ");
rowtitle.CreateCell(3, HSSFCellType.STRING).SetCellValue(" gender ");
rowtitle.CreateCell(4, HSSFCellType.STRING).SetCellValue(" Id number ");
rowtitle.CreateCell(5, HSSFCellType.STRING).SetCellValue(" Employment information identification card number ");
rowtitle.CreateCell(6, HSSFCellType.STRING).SetCellValue(" Name of original institution ");
rowtitle.CreateCell(7, HSSFCellType.STRING).SetCellValue(" Original mechanism number ");
rowtitle.CreateCell(8, HSSFCellType.STRING).SetCellValue(" Change the name of the institution ");
rowtitle.CreateCell(9, HSSFCellType.STRING).SetCellValue(" Change of organization number ");
//DataSet is 1 a DataTale The set, if only filled up 1 A table, then the table ID for 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 = "1 level ";
}
else if (row["R_jb"].ToString() == "2")
{
jibie = "2 level ";
}
else if (row["R_jb"].ToString() == "3")
{
jibie = "3 level ";
}
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;
}
}
Export method:
/// <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 = 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(' generate Excel Failure! ')", true);
}
}