Implement dataTable to Excel file, and the last sharedConvert Excel file to DataTableNegative operations for each other. The conversion of DataTable into Excel file is by passing in a parameter of DataTable type, and then converting the passed DataTable into Excel file. The key codes are as follows:
Method 1:
/// <summary> /// Export DataTable data to Excel/// </summary> /// <param name="data"></param> /// <param name="filepath"></param> public static void DataTableExport(DataTable data, string filepath) { try { //Workbook book = new Workbook("E:\\"); //Open the workbook Workbook book = new Workbook(); //Create a workbook Worksheet sheet = [0]; //Create a worksheet Cells cells = ; //Cell //Create style style = [()]; [].LineStyle = ; //Application boundary line Left boundary line [].LineStyle = ; //Apply the boundary line Right boundary line [].LineStyle = ; //Application boundary line upper boundary line [].LineStyle = ; //Application boundary line Lower boundary line = ; //Horizontal alignment of cell content text centered = "Songyi"; //Font = true; //Set bold = 11; //Set font size // = (153, 204, 0); // Background color // = ; // Background style // = true; // Cell contents automatically wrap //File data int Colnum = ;//Table column count int Rownum = ;//Table row count //Generate row column name row for (int i = 0; i < Colnum; i++) { cells[0, i].PutValue([i].ColumnName); //Add a header cells[0, i].SetStyle(style); //Add style //(i, [i]. * 2 + 1.5); //Custom column width //(0, 30); //Custom high } //Generate data row for (int i = 0; i < Rownum; i++) { for (int k = 0; k < Colnum; k++) { cells[1 + i, k].PutValue([i][k].ToString()); //Add data cells[1 + i, k].SetStyle(style); //Add style } cells[1 + i, 5].Formula = "=B" + (2 + i) + "+C" + (2 + i);//Set calculation formulas for cells to calculate the total number of students in the class } (); //Adaptive width (filepath); //save (); } catch (Exception e) { ("An error occurred when generating excel:" + ); } }
Method 2:
public void ToExcel(DataTable dt) { #region 3s var FilePath = @"D:\ToExcel\";//Generate Excel file path DeleteFile(FilePath); //Create a brand new workbook var workbook = new HSSFWorkbook();//A sheet can be up to 65536 lines var count = 0; for (double i = 0; i < () / (65534); i++)//One tab of each Excel file can only store 65536 lines of data { var row_index = 0; //Create Sheet ("Sheet" + i); //Get the Sheet object based on the Sheet name var sheet = ("Sheet" + i); IRow row; row = (row_index); //Write the title for (int j = 0; j < ; j++) { (j).SetCellValue([j].()); } row = (++row_index); //Write data for (int j = 0; j < ( - count > 65534 ? 65534 : - count); j++) { var r = [j + count]; for (int k = 0; k <; k++) { (k).SetCellValue(r[k].ToString()); //If it is a number, determine whether it needs to be converted to a number //if (IsNumeric(r[k].ToString())) //{ // (k).SetCellValue((r[k].ToString())); //} //else //{ // (k).SetCellValue(r[k].ToString()); //} } row = (++row_index); } count += row_index - 2; } //Save Workbook method 1: Save it to the server in file form (a file will be generated every time you export it, use it with caution) var FileName = ("yyyyMMddHHmmss") + ".xls"; var sw = (FilePath + FileName); (sw); (); var EC = new ExcelConverter(); (Response, FilePath + FileName); #endregion }
This is all about this article about exporting DataTable into Excel files in C#. I hope it will be helpful to everyone's learning and I hope everyone will support me more.