597 lines
23 KiB
C#
597 lines
23 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using SuperMap.RealEstate.CoreFrameWork;
|
|
using SuperMap.RealEstate.ServiceModel;
|
|
using SuperMap.RealEstate.Utility;
|
|
using SuperMap.RealEstate.Web.UI;
|
|
using SuperMap.RealEstate.Web.UI.WebControls;
|
|
using SuperMap.RealEstate.Web.Utility;
|
|
using MSB = SuperMap.RealEstate.MainTenance.Storage.Business;
|
|
using System.Data;
|
|
using NPOI;
|
|
using NPOI.HPSF;
|
|
using NPOI.HSSF;
|
|
using NPOI.SS.UserModel;
|
|
using NPOI.HSSF.UserModel;
|
|
using NPOI.SS.Util;
|
|
using NPOI.POIFS;
|
|
using NPOI.Util;
|
|
using NPOI.HSSF.Util;
|
|
using NPOI.XSSF.UserModel;
|
|
using System.Configuration;
|
|
using System.Web.Security;
|
|
using System.Web.UI.HtmlControls;
|
|
using System.Web.UI.WebControls.WebParts;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
|
|
namespace SuperMap.RealEstate.MainTenance.Storage.Modules.ProblemCollection
|
|
{
|
|
public class ExcelHelper
|
|
{
|
|
#region 导出方法
|
|
private string fileName = null; //文件名
|
|
private IWorkbook workbook = null;
|
|
private FileStream fs = null;
|
|
private bool disposed;
|
|
|
|
public int DataTableToExcel(string FileName, DataTable data, string sheetName, bool isColumnWritten)
|
|
{
|
|
int i = 0;
|
|
int j = 0;
|
|
int count = 0;
|
|
ISheet sheet = null;
|
|
|
|
if (fileName == null)
|
|
{
|
|
workbook = new NPOI.HSSF.UserModel.HSSFWorkbook();
|
|
//NPOI.SS.UserModel.ISheet sheet = book.CreateSheet("Sheet1");
|
|
}
|
|
else
|
|
{
|
|
fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
|
|
if (fileName.IndexOf(".xlsx") > 0) // 2007版本
|
|
workbook = new XSSFWorkbook();
|
|
else if (fileName.IndexOf(".xls") > 0) // 2003版本
|
|
workbook = new HSSFWorkbook();
|
|
}
|
|
try
|
|
{
|
|
if (workbook != null)
|
|
{
|
|
sheet = workbook.CreateSheet(sheetName);
|
|
}
|
|
else
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
if (isColumnWritten == true) //写入DataTable的列名
|
|
{
|
|
IRow row = sheet.CreateRow(0);
|
|
for (j = 0; j < data.Columns.Count; ++j)
|
|
{
|
|
row.CreateCell(j).SetCellValue(data.Columns[j].ColumnName);
|
|
}
|
|
count = 1;
|
|
}
|
|
else
|
|
{
|
|
count = 0;
|
|
}
|
|
|
|
for (i = 0; i < data.Rows.Count; ++i)
|
|
{
|
|
IRow row = sheet.CreateRow(count);
|
|
for (j = 0; j < data.Columns.Count; ++j)
|
|
{
|
|
row.CreateCell(j).SetCellValue(data.Rows[i][j].ToString());
|
|
}
|
|
++count;
|
|
}
|
|
// 写入到客户端
|
|
System.IO.MemoryStream ms = new System.IO.MemoryStream();
|
|
workbook.Write(ms);
|
|
|
|
HttpContext.Current.Response.Clear();
|
|
HttpContext.Current.Response.Buffer = true;
|
|
HttpContext.Current.Response.Charset = "utf-8";
|
|
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");//设置输出流为简体中文
|
|
HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename=" + FileName + "_{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmssfff")));
|
|
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
|
|
HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
|
|
HttpContext.Current.Response.End();
|
|
workbook = null;
|
|
ms.Close();
|
|
ms.Dispose();
|
|
return count;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("Exception: " + ex.Message);
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
public int DataTableToExcelForEhange(string FileName, DataTable data, string sheetName, bool isColumnWritten)
|
|
{
|
|
int i = 0;
|
|
int j = 0;
|
|
int count = 0;
|
|
ISheet sheet = null;
|
|
|
|
if (fileName == null)
|
|
{
|
|
workbook = new NPOI.HSSF.UserModel.HSSFWorkbook();
|
|
}
|
|
else
|
|
{
|
|
fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
|
|
if (fileName.IndexOf(".xlsx") > 0) // 2007版本
|
|
workbook = new XSSFWorkbook();
|
|
else if (fileName.IndexOf(".xls") > 0) // 2003版本
|
|
workbook = new HSSFWorkbook();
|
|
}
|
|
try
|
|
{
|
|
if (workbook != null)
|
|
{
|
|
sheet = workbook.CreateSheet(sheetName);
|
|
}
|
|
else
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
if (isColumnWritten == true) //写入DataTable的列名
|
|
{
|
|
IRow row = sheet.CreateRow(0);
|
|
for (j = 0; j < data.Columns.Count; ++j)
|
|
{
|
|
row.CreateCell(j).SetCellValue(data.Columns[j].ColumnName);
|
|
}
|
|
count = 1;
|
|
}
|
|
else
|
|
{
|
|
count = 0;
|
|
}
|
|
|
|
for (i = 0; i < data.Rows.Count; ++i)
|
|
{
|
|
IRow row = sheet.CreateRow(count);
|
|
for (j = 0; j < data.Columns.Count; ++j)
|
|
{
|
|
row.CreateCell(j).SetCellValue(data.Rows[i][j].ToString());
|
|
}
|
|
++count;
|
|
}
|
|
// 写入到客户端
|
|
System.IO.MemoryStream ms = new System.IO.MemoryStream();
|
|
workbook.Write(ms);
|
|
|
|
HttpContext.Current.Response.Clear();
|
|
HttpContext.Current.Response.Buffer = true;
|
|
HttpContext.Current.Response.Charset = "utf-8";
|
|
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");//设置输出流为简体中文
|
|
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);
|
|
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
|
|
HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
|
|
HttpContext.Current.Response.End();
|
|
workbook = null;
|
|
ms.Close();
|
|
ms.Dispose();
|
|
return count;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("Exception: " + ex.Message);
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
public static DataTable ExcelImport(string filePath)
|
|
{
|
|
|
|
NPOI.SS.UserModel.IWorkbook book = null;
|
|
|
|
|
|
HSSFWorkbook hssfworkbook;
|
|
|
|
#region //初始化信息
|
|
try
|
|
{
|
|
using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
|
|
{
|
|
try
|
|
{
|
|
book = new NPOI.XSSF.UserModel.XSSFWorkbook(file);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
book = new HSSFWorkbook(file);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw e;
|
|
}
|
|
#endregion
|
|
|
|
NPOI.SS.UserModel.ISheet sheet = book.GetSheetAt(0);
|
|
System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
|
|
DataTable dt = new DataTable();
|
|
for (int j = 0; j < (sheet.GetRow(0).LastCellNum); j++)
|
|
{
|
|
dt.Columns.Add(Convert.ToChar(((int)'A') + j).ToString());
|
|
}
|
|
|
|
|
|
|
|
while (rows.MoveNext())
|
|
{
|
|
Type _Type = rows.Current.GetType();
|
|
IRow row = (IRow)rows.Current;
|
|
//HSSFRow row = (HSSFRow)
|
|
DataRow dr = dt.NewRow();
|
|
for (int i = 0; i < row.LastCellNum; i++)
|
|
{
|
|
NPOI.SS.UserModel.ICell cell = row.GetCell(i);
|
|
if (cell == null)
|
|
{
|
|
dr[i] = null;
|
|
}
|
|
else
|
|
{
|
|
dr[i] = cell.ToString();
|
|
}
|
|
}
|
|
dt.Rows.Add(dr);
|
|
}
|
|
return dt;
|
|
}
|
|
|
|
public int DataTableToExcels(string FileName, DataTable data, string sheetName, bool isColumnWritten, bool isDetail)
|
|
{
|
|
int i = 0;
|
|
int j = 0;
|
|
int count = 0;
|
|
ISheet sheet = null;
|
|
|
|
if (fileName == null)
|
|
{
|
|
workbook = new NPOI.HSSF.UserModel.HSSFWorkbook();
|
|
//NPOI.SS.UserModel.ISheet sheet = book.CreateSheet("Sheet1");
|
|
}
|
|
else
|
|
{
|
|
fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
|
|
if (fileName.IndexOf(".xlsx") > 0) // 2007版本
|
|
workbook = new XSSFWorkbook();
|
|
else if (fileName.IndexOf(".xls") > 0) // 2003版本
|
|
workbook = new HSSFWorkbook();
|
|
}
|
|
try
|
|
{
|
|
if (workbook != null)
|
|
{
|
|
sheet = workbook.CreateSheet(sheetName);
|
|
}
|
|
else
|
|
{
|
|
return -1;
|
|
}
|
|
if (isColumnWritten == true) //写入DataTable的列名
|
|
{
|
|
|
|
IRow row = sheet.CreateRow(0);
|
|
if (!isDetail)
|
|
{
|
|
row.CreateCell(0).SetCellValue("服务区");
|
|
row.CreateCell(1).SetCellValue("所属区域");
|
|
row.CreateCell(2).SetCellValue("项目名称");
|
|
row.CreateCell(3).SetCellValue("经营商户");
|
|
row.CreateCell(4).SetCellValue("应收账款余额");
|
|
row.CreateCell(5).SetCellValue("账期<一个月");
|
|
row.CreateCell(6).SetCellValue("一个月<账期<三个月");
|
|
row.CreateCell(7).SetCellValue("三个月<账期");
|
|
row.CreateCell(8).SetCellValue("其他应收款余额");
|
|
row.CreateCell(9).SetCellValue("账期<一个月");
|
|
row.CreateCell(10).SetCellValue("一个月<账期<三个月");
|
|
row.CreateCell(11).SetCellValue("三个月<账期");
|
|
row.CreateCell(12).SetCellValue("逾期金额合计");
|
|
row.CreateCell(13).SetCellValue("逾期原因分析");
|
|
row.CreateCell(14).SetCellValue("备注");
|
|
}
|
|
else
|
|
{
|
|
row.CreateCell(0).SetCellValue("所属区域");
|
|
row.CreateCell(1).SetCellValue("服务区");
|
|
row.CreateCell(2).SetCellValue("应收账款合计");
|
|
row.CreateCell(3).SetCellValue("正常(账期<1个月)");
|
|
row.CreateCell(4).SetCellValue("异常(1个月<账期<3个月)");
|
|
row.CreateCell(5).SetCellValue("严重逾期(3个月<账期)");
|
|
row.CreateCell(6).SetCellValue("其他应收款余额");
|
|
row.CreateCell(7).SetCellValue("正常(账期<1个月)");
|
|
row.CreateCell(8).SetCellValue("异常(1个月<账期<3个月)");
|
|
row.CreateCell(9).SetCellValue("严重逾期(3个月<账期)");
|
|
}
|
|
count = 1;
|
|
}
|
|
else
|
|
{
|
|
count = 0;
|
|
}
|
|
|
|
for (i = 0; i < data.Rows.Count; ++i)
|
|
{
|
|
IRow row = sheet.CreateRow(count);
|
|
for (j = 0; j < data.Columns.Count; ++j)
|
|
{
|
|
row.CreateCell(j).SetCellValue(data.Rows[i][j].ToString());
|
|
}
|
|
++count;
|
|
}
|
|
// 写入到客户端
|
|
System.IO.MemoryStream ms = new System.IO.MemoryStream();
|
|
workbook.Write(ms);
|
|
|
|
HttpContext.Current.Response.Clear();
|
|
HttpContext.Current.Response.Buffer = true;
|
|
HttpContext.Current.Response.Charset = "utf-8";
|
|
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");//设置输出流为简体中文
|
|
HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename=" + FileName +
|
|
"_{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmssfff")));
|
|
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
|
|
HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
|
|
HttpContext.Current.Response.End();
|
|
workbook = null;
|
|
ms.Close();
|
|
ms.Dispose();
|
|
return count;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("Exception: " + ex.Message);
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
public void setCellStyleAlignmentCenter(HSSFCellStyle style)
|
|
{
|
|
style.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
|
|
style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
|
|
}
|
|
|
|
public HSSFFont createFont(HSSFWorkbook hssfworkbook)
|
|
{
|
|
return (HSSFFont)hssfworkbook.CreateFont();
|
|
}
|
|
|
|
public void setCellStyleBord(HSSFCellStyle style)
|
|
{
|
|
|
|
style.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
style.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
style.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
style.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
}
|
|
#endregion
|
|
|
|
#region 导入方法
|
|
/// <summary>读取excel
|
|
/// 默认第一行为表头,导入第一个工作表
|
|
/// </summary>
|
|
/// <param name="strFileName">excel文档路径</param>
|
|
/// <returns></returns>
|
|
public static DataTable Import(string strFileName)
|
|
{
|
|
DataTable dt = new DataTable();
|
|
|
|
HSSFWorkbook hssfworkbook;
|
|
using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read))
|
|
{
|
|
hssfworkbook = new HSSFWorkbook(file);
|
|
}
|
|
ISheet sheet = hssfworkbook.GetSheetAt(0);
|
|
System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
|
|
|
|
IRow headerRow = sheet.GetRow(0);
|
|
int cellCount = headerRow.LastCellNum;
|
|
|
|
for (int j = 0; j < cellCount; j++)
|
|
{
|
|
ICell cell = headerRow.GetCell(j);
|
|
dt.Columns.Add(cell.ToString());
|
|
}
|
|
|
|
for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
|
|
{
|
|
IRow row = sheet.GetRow(i);
|
|
DataRow dataRow = dt.NewRow();
|
|
|
|
for (int j = row.FirstCellNum; j < cellCount; j++)
|
|
{
|
|
if (row.GetCell(j) != null)
|
|
dataRow[j] = row.GetCell(j).ToString();
|
|
}
|
|
dt.Rows.Add(dataRow);
|
|
}
|
|
return dt;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从Excel中获取数据到DataTable
|
|
/// </summary>
|
|
/// <param name="strFileName">Excel文件全路径(服务器路径)</param>
|
|
/// <param name="SheetName">要获取数据的工作表名称</param>
|
|
/// <param name="HeaderRowIndex">工作表标题行所在行号(从0开始)</param>
|
|
/// <returns></returns>
|
|
public static DataTable RenderDataTableFromExcel(string strFileName, string SheetName, int HeaderRowIndex)
|
|
{
|
|
using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read))
|
|
{
|
|
IWorkbook workbook = new HSSFWorkbook(file);
|
|
ISheet sheet = workbook.GetSheet(SheetName);
|
|
return RenderDataTableFromExcel(workbook, SheetName, HeaderRowIndex);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从Excel中获取数据到DataTable
|
|
/// </summary>
|
|
/// <param name="strFileName">Excel文件全路径(服务器路径)</param>
|
|
/// <param name="SheetIndex">要获取数据的工作表序号(从0开始)</param>
|
|
/// <param name="HeaderRowIndex">工作表标题行所在行号(从0开始)</param>
|
|
/// <returns></returns>
|
|
public static DataTable RenderDataTableFromExcel(string strFileName, int SheetIndex, int HeaderRowIndex)
|
|
{
|
|
using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read))
|
|
{
|
|
IWorkbook workbook = new HSSFWorkbook(file);
|
|
string SheetName = workbook.GetSheetName(SheetIndex);
|
|
return RenderDataTableFromExcel(workbook, SheetName, HeaderRowIndex);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从Excel中获取数据到DataTable
|
|
/// </summary>
|
|
/// <param name="ExcelFileStream">Excel文件流</param>
|
|
/// <param name="SheetName">要获取数据的工作表名称</param>
|
|
/// <param name="HeaderRowIndex">工作表标题行所在行号(从0开始)</param>
|
|
/// <returns></returns>
|
|
public static DataTable RenderDataTableFromExcel(Stream ExcelFileStream, string SheetName, int HeaderRowIndex)
|
|
{
|
|
IWorkbook workbook = new HSSFWorkbook(ExcelFileStream);
|
|
ExcelFileStream.Close();
|
|
return RenderDataTableFromExcel(workbook, SheetName, HeaderRowIndex);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从Excel中获取数据到DataTable
|
|
/// </summary>
|
|
/// <param name="ExcelFileStream">Excel文件流</param>
|
|
/// <param name="SheetIndex">要获取数据的工作表序号(从0开始)</param>
|
|
/// <param name="HeaderRowIndex">工作表标题行所在行号(从0开始)</param>
|
|
/// <returns></returns>
|
|
public static DataTable RenderDataTableFromExcel(Stream ExcelFileStream, int SheetIndex, int HeaderRowIndex)
|
|
{
|
|
IWorkbook workbook = new HSSFWorkbook(ExcelFileStream);
|
|
ExcelFileStream.Close();
|
|
string SheetName = workbook.GetSheetName(SheetIndex);
|
|
return RenderDataTableFromExcel(workbook, SheetName, HeaderRowIndex);
|
|
}
|
|
/// <summary>
|
|
/// 从Excel中获取数据到DataTable
|
|
/// </summary>
|
|
/// <param name="workbook">要处理的工作薄</param>
|
|
/// <param name="SheetName">要获取数据的工作表名称</param>
|
|
/// <param name="HeaderRowIndex">工作表标题行所在行号(从0开始)</param>
|
|
/// <returns></returns>
|
|
public static DataTable RenderDataTableFromExcel(IWorkbook workbook, string SheetName, int HeaderRowIndex)
|
|
{
|
|
ISheet sheet = workbook.GetSheet(SheetName);
|
|
DataTable table = new DataTable();
|
|
try
|
|
{
|
|
IRow headerRow = sheet.GetRow(HeaderRowIndex);
|
|
int cellCount = headerRow.LastCellNum;
|
|
|
|
for (int i = headerRow.FirstCellNum; i < cellCount; i++)
|
|
{
|
|
DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue);
|
|
table.Columns.Add(column);
|
|
}
|
|
|
|
int rowCount = sheet.LastRowNum;
|
|
|
|
#region 循环各行各列,写入数据到DataTable
|
|
for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
|
|
{
|
|
IRow row = sheet.GetRow(i);
|
|
DataRow dataRow = table.NewRow();
|
|
for (int j = row.FirstCellNum; j < cellCount; j++)
|
|
{
|
|
ICell cell = row.GetCell(j);
|
|
if (cell == null)
|
|
{
|
|
dataRow[j] = null;
|
|
}
|
|
else
|
|
{
|
|
//dataRow[j] = cell.ToString();
|
|
switch (cell.CellType)
|
|
{
|
|
case CellType.Blank:
|
|
dataRow[j] = null;
|
|
break;
|
|
case CellType.Boolean:
|
|
dataRow[j] = cell.BooleanCellValue;
|
|
break;
|
|
case CellType.Numeric:
|
|
dataRow[j] = cell.ToString();
|
|
break;
|
|
case CellType.String:
|
|
dataRow[j] = cell.StringCellValue;
|
|
break;
|
|
case CellType.Error:
|
|
dataRow[j] = cell.ErrorCellValue;
|
|
break;
|
|
case CellType.Formula:
|
|
default:
|
|
dataRow[j] = "=" + cell.CellFormula;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
table.Rows.Add(dataRow);
|
|
//dataRow[j] = row.GetCell(j).ToString();
|
|
}
|
|
#endregion
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
table.Clear();
|
|
table.Columns.Clear();
|
|
table.Columns.Add("出错了");
|
|
DataRow dr = table.NewRow();
|
|
dr[0] = ex.Message;
|
|
table.Rows.Add(dr);
|
|
return table;
|
|
}
|
|
finally
|
|
{
|
|
//sheet.Dispose();
|
|
workbook = null;
|
|
sheet = null;
|
|
}
|
|
#region 清除最后的空行
|
|
for (int i = table.Rows.Count - 1; i > 0; i--)
|
|
{
|
|
bool isnull = true;
|
|
for (int j = 0; j < table.Columns.Count; j++)
|
|
{
|
|
if (table.Rows[i][j] != null)
|
|
{
|
|
if (table.Rows[i][j].ToString() != "")
|
|
{
|
|
isnull = false;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (isnull)
|
|
{
|
|
table.Rows[i].Delete();
|
|
}
|
|
}
|
|
#endregion
|
|
return table;
|
|
}
|
|
#endregion
|
|
}
|
|
} |