Create excel files using c without installing excel

  • 2020-06-12 10:30:43
  • OfStack


// create excel
object missing = System.Reflection.Missing.Value;
Excel.Application app = new Excel.Application();
app.Application.Workbooks.Add(true);
Excel.Workbook book = (Excel.Workbook)app.ActiveWorkbook;
Excel.Worksheet sheet = (Excel.Worksheet)book.ActiveSheet;

#region  The first 1 line 
sheet.Cells[1, 1] = " Login name (loginID)";
sheet.Cells[1, 2] = " password (passWord)";
sheet.Cells[1, 3] = " The surname (familyName)";
sheet.Cells[1, 4] = " The name (firstName)";
sheet.Cells[1, 5] = " gender (gender)";
sheet.Cells[1, 6] = " Time of birth (dateofBirth)";
sheet.Cells[1, 7] = " Mobile phone no. (cellphoneNum)";
sheet.Cells[1, 8] = " Id number (identityID)";
sheet.Cells[1, 9] = " Employment status (jobStatus)";
sheet.Cells[1, 10] = " The phone company (telephoneNum)";
sheet.Cells[1, 11] = " email (email)";
sheet.Cells[1, 12] = " Ancestral home (nativeHome)";
sheet.Cells[1, 13] = " Graduated from the school (graduateSchool)";
sheet.Cells[1, 14] = " professional (major)";
sheet.Cells[1, 15] = " Graduation of time (graduateTime)";
sheet.Cells[1, 16] = " Record of formal schooling (education)";
sheet.Cells[1, 17] = " Zip code (zipCode)";
sheet.Cells[1, 18] = " address (address)";
sheet.Cells[1, 19] = " In the time (entryTime)";
sheet.Cells[1, 20] = " Leave time (leaveTime)";
sheet.Cells[1, 21] = " note (remarks)";
sheet.Cells[1, 22] = " department (departmentID)";
sheet.Cells[1, 23] = " position (JobTypeID";
#endregion

#region  Circular write content 
int count = 1;
foreach (EmployeeInfo_tbl item in enterpriseInfo.Employees)
{
count = count+1;
sheet.Cells[count, 1] = item.loginID;
sheet.Cells[count, 2] = item.passWord;
sheet.Cells[count, 3] = item.familyName;//" The surname (familyName)";
sheet.Cells[count, 4] = item.firstName; //" The name (firstName)";
sheet.Cells[count, 5] = item.gender; //" gender (gender)";
sheet.Cells[count, 6] = item.dateofBirth; //" Time of birth (dateofBirth)";
sheet.Cells[count, 7] = item.cellphoneNum;//" Mobile phone no. (cellphoneNum)";
sheet.Cells[count, 8] = item.identityID;//" Id number (identityID)";
sheet.Cells[count, 9] = item.jobStatus;//" Employment status (jobStatus)";
sheet.Cells[count, 10] = item.telephoneNum;//" The phone company (telephoneNum)";
sheet.Cells[count, 11] = item.email;//" email (email)";
sheet.Cells[count, 12] = item.nativeHome;//" Ancestral home (nativeHome)";
sheet.Cells[count, 13] = item.graduateSchool;// " Graduated from the school (graduateSchool)";
sheet.Cells[count, 14] = item.major;// " professional (major)";
sheet.Cells[count, 15] = item.graduateTime;//" Graduation of time (graduateTime)";
sheet.Cells[count, 16] = item.education;// " Record of formal schooling (education)";
sheet.Cells[count, 17] = item.zipCode;// " Zip code (zipCode)";
sheet.Cells[count, 18] = item.address;//" address (address)";
sheet.Cells[count, 19] = item.entryTime;//" In the time (entryTime)";
sheet.Cells[count, 20] = item.leaveTime;// " Leave time (leaveTime)";
sheet.Cells[count, 21] = item.remarks;// " note (remarks)";
sheet.Cells[count, 22] = item.Department.departmentName;// " department (departmentID)";
sheet.Cells[count, 23] = item.JobType.jobName;// " position (JobTypeID";
}
#endregion
// save 
//book.SaveCopyAs(_FolderBrowserDialog.SelectedPath + @"\test.xls");
// Close the file 
//book.Close(false, missing, missing);
// exit excel
//app.Quit();

You need to refer to Microsoft Excel 14.0 Object Libary in com (other versions do much the same)

Which of course means you have to install office Excel to do this,

If you need to spare office Excel then look at my final implementation method ~!

My final implementation was using the third side Aspose.Cells.dll

This dll1 is free of charge,(third party is at risk, use with caution)


// create excel
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
Aspose.Cells.Worksheet sheet = workbook.Worksheets[0];
sheet.FreezePanes(1, 1, 1, 0);// Freeze the first 1 line 

#region  The first 1 line 
sheet.Cells["A1"].PutValue(" Login name (loginID)");
sheet.Cells["B1"].PutValue(" password (passWord)");
sheet.Cells["C1"].PutValue(" The surname (familyName)");
sheet.Cells["D1"].PutValue(" The name (firstName)");
sheet.Cells["E1"].PutValue(" gender (gender)");
sheet.Cells["F1"].PutValue(" Time of birth (dateofBirth)");
sheet.Cells["G1"].PutValue(" Mobile phone no. (cellphoneNum)");
sheet.Cells["H1"].PutValue(" Id number (identityID)");
sheet.Cells["I1"].PutValue(" Employment status (jobStatus)");
sheet.Cells["J1"].PutValue(" The phone company (telephoneNum)");
sheet.Cells["K1"].PutValue(" email (email)");
sheet.Cells["L1"].PutValue(" Ancestral home (nativeHome)");
sheet.Cells["M1"].PutValue(" Graduated from the school (graduateSchool)");
sheet.Cells["N1"].PutValue(" professional (major)");
sheet.Cells["O1"].PutValue(" Graduation of time (graduateTime)");
sheet.Cells["P1"].PutValue(" Record of formal schooling (education)");
sheet.Cells["Q1"].PutValue(" Zip code (zipCode)");
sheet.Cells["R1"].PutValue(" address (address)");
sheet.Cells["S1"].PutValue(" In the time (entryTime)");
sheet.Cells["T1"].PutValue(" Leave time (leaveTime)");
sheet.Cells["U1"].PutValue(" note (remarks)");
sheet.Cells["V1"].PutValue(" department (departmentID)");
sheet.Cells["W1"].PutValue(" position (JobTypeID");
#endregion

#region  Circular write content 
int count = 1;
foreach (EmployeeInfo_tbl item in enterpriseInfo.Employees)
{
 count = count + 1;
 sheet.Cells["A" + count].PutValue(item.loginID);
 sheet.Cells["B" + count].PutValue(item.passWord);
 sheet.Cells["C" + count].PutValue(item.familyName);//" The surname (familyName)";
 sheet.Cells["D" + count].PutValue(item.firstName); //" The name (firstName)";
 sheet.Cells["E" + count].PutValue(item.gender == 0 ? " female " : " male "); //" gender (gender)";
 sheet.Cells["F" + count].PutValue(item.dateofBirth.ToString() == "" ? null : item.dateofBirth.ToString()); //" Time of birth (dateofBirth)";
 sheet.Cells["G" + count].PutValue(item.cellphoneNum.ToString());//" Mobile phone no. (cellphoneNum)";
 sheet.Cells["H" + count].PutValue(item.identityID);//" Id number (identityID)";
 sheet.Cells["I" + count].PutValue(item.jobStatus == 1 ? " on-the-job " : " departure ");//" Employment status (jobStatus)";
 sheet.Cells["J" + count].PutValue(item.telephoneNum);//" The phone company (telephoneNum)";
 sheet.Cells["K" + count].PutValue(item.email);//" email (email)";
 sheet.Cells["L" + count].PutValue(item.nativeHome);//" Ancestral home (nativeHome)";
 sheet.Cells["M" + count].PutValue(item.graduateSchool);// " Graduated from the school (graduateSchool)";
 sheet.Cells["N" + count].PutValue(item.major);// " professional (major)";
 sheet.Cells["O" + count].PutValue(item.graduateTime.ToString() == "" ? null : item.graduateTime.ToString());//" Graduation of time (graduateTime)";
 string ed = "";
 switch (item.education)
 {
  case 1:
ed = " Junior high school / Primary school ";
break;
  case 2:
ed = " High school / Technical secondary school ";
break;
  case 3:
ed = " Undergraduate course / Specialized subject ";
break;
  case 4:
ed = " Graduate or above ";
break;
  default:
ed = null;
break;
 }
 sheet.Cells["P" + count].PutValue(ed);// " Record of formal schooling (education)";
 sheet.Cells["Q" + count].PutValue(item.zipCode);// " Zip code (zipCode)";
 sheet.Cells["R" + count].PutValue(item.address);//" address (address)";
 sheet.Cells["S" + count].PutValue(item.entryTime.ToString() == "" ? null : item.entryTime.ToString());//" In the time (entryTime)";
 sheet.Cells["T" + count].PutValue(item.leaveTime.ToString() == "" ? null : item.leaveTime.ToString());// " Leave time (leaveTime)";
 sheet.Cells["U" + count].PutValue(item.remarks);// " note (remarks)";
 sheet.Cells["V" + count].PutValue(item.Department.departmentName);// " department (departmentID)";
 sheet.Cells["W" + count].PutValue(item.JobType.jobName);// " position (JobTypeID";
}
#endregion

// save 
workbook.Save(_FolderBrowserDialog.SelectedPath + @"\test.xls");


Related articles: