[点晴永久免费OA]【C#】NPOI导出Excel,包含一个或者多个sheet表
当前位置:点晴教程→点晴OA办公管理信息系统
→『 经验分享&问题答疑 』
using NPOI.XSSF.UserModel;
using System;
using System.Data;
using System.IO;
using System.Web;
using NPOI.SS.UserModel;
namespace C.Customization.Helper
{
/// <summary>
/// Class execlHelper.
/// </summary>
public class execlHelper
{
/// <summary>
/// Datas the set to excel.
/// </summary>
/// <param name="ds">The ds.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
public static string DataSetToExcel(DataSet ds)
{
try
{
string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xlsx";
string urlPath = "Upload/ExcelFiles/";
string filePath = HttpContext.Current.Server.MapPath("\\" + urlPath);
Directory.createDirectory(filePath);
string Path = filePath + fileName;
FileStream fs = null;
XSSFWorkbook workbook = new XSSFWorkbook();
for (int i = 0; i < ds.Tables.Count; i++)
{
XSSFSheet sheet = (XSSFSheet)workbook.createSheet(ds.Tables[i].TableName);
XSSFCellStyle dateStyle = (XSSFCellStyle)workbook.createCellStyle();
XSSFDataFormat format = (XSSFDataFormat)workbook.createDataFormat();
dateStyle.DataFormat = format.GetFormat("yyyy-mm-dd");
int rowIndex = 0;
#region 新建表,填充表头,填充列头,样式
if (rowIndex == 0)
{
#region 列头及样式
XSSFRow headerRow = (XSSFRow)sheet.createRow(0);
XSSFCellStyle headStyle = (XSSFCellStyle)workbook.createCellStyle();
headStyle.Alignment = HorizontalAlignment.Center;
XSSFFont font = (XSSFFont)workbook.createFont();
font.FontHeightInPoints = 12;
font.Boldweight = 700;
headStyle.SetFont(font);
//自定义表头
for (var j = 0; j < ds.Tables[i].Columns.Count; j++)
{
sheet.SetColumnWidth(j, 30 * 256);
headerRow.createCell(j).SetCellValue(ds.Tables[i].Columns[j].ColumnName);
headerRow.GetCell(j).CellStyle = headStyle;
}
#endregion
rowIndex = 1;
}
#endregion
ICellStyle cellstyle = workbook.createCellStyle();
cellstyle.VerticalAlignment = VerticalAlignment.Center;
cellstyle.Alignment = HorizontalAlignment.Center;
foreach (DataRow row in ds.Tables[i].Rows)
{
XSSFRow dataRow = (XSSFRow)sheet.createRow(rowIndex);
#region 填充内容
foreach (DataColumn column in ds.Tables[i].Columns)
{
XSSFCell newCell = (XSSFCell)dataRow.createCell(column.Ordinal);
string type = row[column].GetType()?.FullName;
newCell.SetCellValue(GetValue(row[column].ToString(), type));
newCell.CellStyle = cellstyle;
}
#endregion
rowIndex++;
}
}
using (fs = File.OpenWrite(Path))
{
workbook.Write(fs);
return Path;
}
}
catch
{
return "";
}
}
/// <summary>
/// Gets the value.
/// </summary>
/// <param name="cellValue">The cell value.</param>
/// <param name="type">The type.</param>
/// <returns>System.String.</returns>
private static string GetValue(string cellValue, string type)
{
object value = string.Empty;
switch (type)
{
case "System.String"://字符串类型
value = cellValue;
break;
case "System.DateTime"://日期类型
System.DateTime dateV;
System.DateTime.TryParse(cellValue, out dateV);
value = dateV;
break;
case "System.Boolean"://布尔型
bool boolV = false;
bool.TryParse(cellValue, out boolV);
value = boolV;
break;
case "System.Int16"://整型
case "System.Int32":
case "System.Int64":
case "System.Byte":
int intV = 0;
int.TryParse(cellValue, out intV);
value = intV;
break;
case "System.Decimal"://浮点型
case "System.Double":
double doubV = 0;
double.TryParse(cellValue, out doubV);
value = doubV;
break;
case "System.DBNull"://空值处理
value = string.Empty;
break;
default:
value = string.Empty;
break;
}
return value.ToString();
}
}
}该文章在 2022/11/25 15:21:04 编辑过 |
关键字查询
相关文章
正在查询... |