ComplianceServer/oldcode/Common/ExcelHandler.cs

216 lines
7.8 KiB
C#

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Data;
using System.IO;
using System.Web;
namespace WX.CRM.Common
{
public class ExcelHandler
{
/// <summary>
/// 由DataSet导出Excel
/// </summary>
/// <param name="sourceDs"></param>
/// <param name="fileName">指定Excel工作表名称</param>
/// <param name="sheetName"></param>
/// <returns>Excel工作表</returns>
public static void ExportDataSetToExcel(DataSet sourceDs, string fileName, string sheetName)
{
MemoryStream ms = ExportDataSetToExcel(sourceDs, sheetName) as MemoryStream;
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
HttpContext.Current.Response.End();
ms.Close();
ms = null;
}
/// <summary>
/// 由DataSet导出Excel
/// </summary>
/// <param name="sourceDs">数据源</param>
/// <param name="folderPath">文件夹名</param>
/// <param name="fileName">文件名</param>
/// <param name="sheetName">指定Excel工作表名称</param>
public static void SaveDataSetToExcel(DataSet sourceDs, string folderPath, string fileName, string sheetName)
{
Stream stream = ExportDataSetToExcel(sourceDs, sheetName);
FileUnit.WriteWithDirectory(folderPath, fileName, stream);
}
/// <summary>
/// 由DataSet导出Excel
/// </summary>
/// <param name="sourceTable">要导出数据的DataTable</param>
/// <param name="sheetName">工作表名称</param>
/// <returns>Excel工作表</returns>
private static Stream ExportDataSetToExcel(DataSet sourceDs, string sheetName)
{
HSSFWorkbook workbook = new HSSFWorkbook();
MemoryStream ms = new MemoryStream();
string[] sheetNames = sheetName.Split(',');
int sheetSize = 65530;
int rowCount = sourceDs.Tables[0].Rows.Count;
int sheetCout = (rowCount / sheetSize) + 1;
for (int i = 0; i < sheetNames.Length; i++)
{
for (int j = 0; j < sheetCout; j++)
{
ISheet sheet = workbook.CreateSheet(sheetNames[i] + j);
IRow headerRow = sheet.CreateRow(0);
// handling header.
foreach (DataColumn column in sourceDs.Tables[i].Columns)
headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
// handling value.
int rowIndex = 1;
int mcount = (j + 1) * sheetSize > rowCount ? rowCount : (j + 1) * sheetSize;
for (int m = (j * sheetSize); m < mcount; m++)
{
DataRow row = sourceDs.Tables[i].Rows[m];
IRow dataRow = sheet.CreateRow(rowIndex);
foreach (DataColumn column in sourceDs.Tables[i].Columns)
{
ICell icell = dataRow.CreateCell(column.Ordinal);
ICellValeSet(column.DataType.FullName, row[column].ToString(), icell);
//dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
}
rowIndex++;
}
}
}
workbook.Write(ms);
ms.Flush();
ms.Position = 0;
workbook = null;
return ms;
}
private static void ICellValeSet(string dbType, string dbValue, ICell icell)
{
switch (GetCellType(dbType))
{
case stylexls.:
int wovalue = 0;
if (int.TryParse(dbValue, out wovalue))
{
icell.SetCellValue(wovalue);
}
else
{
icell.SetCellValue(dbValue);
}
break;
case stylexls.:
double dobValue = 0;
if (double.TryParse(dbValue, out dobValue))
{
icell.SetCellValue(dobValue);
}
else
{
icell.SetCellValue(dbValue);
}
break;
case stylexls.:
DateTime timeValue;
if (DateTime.TryParse(dbValue, out timeValue))
{
icell.SetCellValue(timeValue.ToString("yyyy/MM/dd HH:mm:ss"));
}
else
{
icell.SetCellValue("");
}
break;
case stylexls.: icell.SetCellValue(dbValue.ToString()); break;
}
}
private static void ICellValeSet(ExcelCell cell, ICell icell)
{
switch (GetCellType(cell.type))
{
case stylexls.:
int wovalue = 0;
if (int.TryParse(cell.nvalue, out wovalue))
{
icell.SetCellValue(wovalue);
}
else
{
icell.SetCellValue(cell.nvalue);
}
break;
case stylexls.:
double dobValue = 0;
if (double.TryParse(cell.nvalue, out dobValue))
{
icell.SetCellValue(dobValue);
}
else
{
icell.SetCellValue(cell.nvalue);
}
break;
case stylexls.:
DateTime timeValue;
if (DateTime.TryParse(cell.nvalue, out timeValue))
{
icell.SetCellValue(timeValue.ToString("yyyy/MM/dd HH:mm:ss"));
}
else
{
icell.SetCellValue("");
}
break;
case stylexls.: icell.SetCellValue(cell.nvalue.ToString()); break;
}
}
private static stylexls GetCellType(string fullTypeName)
{
if (fullTypeName == null)
return stylexls.;
if (fullTypeName.IndexOf("System.Decimal") > -1)
return stylexls.;
if (fullTypeName.IndexOf("System.Int") > -1)
return stylexls.;
if (fullTypeName.IndexOf("System.DateTime") > -1)
return stylexls.;
return stylexls.;
}
}
#region
public enum stylexls
{
,
url,
,
,
,
,
,
,
,
}
#endregion
public class ExcelCell
{
/// <summary>
/// 英文字段名称
/// </summary>
public string fild { get; set; }
/// <summary>
/// 字段的数据类型
/// </summary>
public string type { get; set; }
/// <summary>
/// 字段的中文标题
/// </summary>
public string title { get; set; }
/// <summary>
/// 字段的值
/// </summary>
public string nvalue { get; set; }
}
}