.net download data to Excel file

List<Model> list = HttpPost<List<Model>>(" ", paramModel);
if (list != null && list.Count > 0)
{

DataTable tblDatas = new DataTable("Datas");
DataColumn dc = null;
dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
dc.AutoIncrement = true;//自动增加
dc.AutoIncrementSeed = 1;//起始为1
dc.AutoIncrementStep = 1;//步长为1
dc.AllowDBNull = false;//

dc = tblDatas.Columns.Add("推广位ID", typeof(string));

foreach (var item in list)
{

DataRow newRow;
newRow = tblDatas.NewRow();
newRow["推广位ID"] = item.PositionId;

 tblDatas.Rows.Add(newRow);

}

var result = NPOIHelper.ExportDataTableToExcel(tblDatas);

return File(result, "application/ms-excel", "统计.xls");

}

 

Stream ExportDataTableToExcel static public (the DataTable sourceTable)
{
HSSFWorkbook Workbook new new HSSFWorkbook = ();
the MemoryStream the MemoryStream new new MS = ();
ISheet Sheet workbook.CreateSheet = (); // Create Worksheet
the IRow headerRow = sheet.CreateRow (0);
/ / column names
List <string> arrHeader = new List <string> (); // Get the column names (column names plurality of character string) to the separator, achieved (ID | number, the name | name)
the foreach (the DataColumn Item sourceTable.Columns in)
{
arrHeader.Add (item.ColumnName);
}
string [] = arrParam new new string [arrHeader.Count]; // array to store a single column name string
string [] arrName = new string [ arrParam.Length ]; // database column names
string [] arrType = new string [ arrParam.Length]; // Excel in cell types
for (int I = 0; I <arrHeader.Count; I ++)
{
arrParam = arrHeader [i] .Split ( '|'); //// 1 to obtain a single array of strings obtaining names and column names in Chinese as delimiters (ID, ID)
headerRow.CreateCell (I) .SetCellValue (arrParam [0]); // set the column names
arrName [i] = arrParam [0 ] .ToString (); // the name of a database column in the array is added

// whether to pass the field type
IF (arrParam.Length> 2)
{
arrType [I] = arrParam [2] .ToString (); // Type field
}
}
// Data
int rowIndex = 1;

// cell format
ICellStyle workbook.CreateCellStyle CellStyle = ();
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat ( "0.00");

IF (! = null && sourceTable.Rows.Count sourceTable> 0)
{
the foreach (in the DataRow Row sourceTable.Rows) // cyclic data lines
{
the IRow DataRow = sheet.CreateRow (rowIndex);
the foreach (column in the DataColumn sourceTable.Columns) // loop column, remove each row of each column
{
for (int I = 0; I <arrName.Length; I ++) // cycle field to be displayed, out on the configuration dataRow
{
IF (column.ColumnName.ToLower () == arrName [I] .ToLower ())
{
// If the field type mass
IF (arrParam.Length> 2)
{
ICell Cell dataRow.CreateCell = (I);
Switch (arrType [I] .Replace ( " \ R & lt \ n-"," ") .trim ())
{
Case" int ":
! IF (String.IsNullOrEmpty (Row [column] .ToString ()))
{
cell.SetCellValue(Convert.ToInt64(row[column].ToString()));
}
else
{
cell.SetCellValue(row[column].ToString());
}
break;
case "string":
cell.SetCellValue(row[column].ToString());
break;
case "datetime":
cell.SetCellValue(row[column].ToString());
break;
case "decimal":
if (!string.IsNullOrEmpty(row[column].ToString()))
{
cell.SetCellValue(Convert.ToInt64(Convert.ToDecimal(row[column].ToString())));
}
else
{
cell.SetCellValue(row[column].ToString());
}

= CellStyle cell.CellStyle;
BREAK;
}
}
the else
{
dataRow.CreateCell (I) .SetCellValue (Row [column] .ToString ()); // data string to be displayed in the row added to Excel
}
BREAK;
}
}
}
rowIndex ++ ;
}
}
workbook.Write (MS);
ms.Flush ();
ms.Position = 0;
Sheet = null;
headerRow = null;
Workbook = null;
return MS;
}

 

Guess you like

Origin www.cnblogs.com/xiaoyao123/p/11579764.html