You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
205 lines
7.5 KiB
205 lines
7.5 KiB
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.IO;
|
|
using System.Text.RegularExpressions;
|
|
using NPOI.SS.UserModel;
|
|
using NPOI.HSSF.UserModel;
|
|
using NPOI.XSSF.UserModel;
|
|
using System.Data;
|
|
using System.Web;
|
|
|
|
namespace QMAPP.Web.Common
|
|
{
|
|
/// <summary>
|
|
/// Excel操作工具
|
|
/// 创建者:单雨春
|
|
/// 创建日期:2014年9月26日
|
|
/// </summary>
|
|
public partial class ExcelOperationHelper
|
|
{
|
|
#region 获得临时路径
|
|
/// <summary>
|
|
/// 获得临时路径
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static string GetTempPath()
|
|
{
|
|
//应用程序物理路径
|
|
var PhysicsRootPath = System.Web.HttpContext.Current.Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath);
|
|
|
|
//临时文件路径
|
|
var TempPath = PhysicsRootPath + "Temp\\";
|
|
|
|
if (System.IO.Directory.Exists(PhysicsRootPath + "Temp") == false)
|
|
{
|
|
System.IO.Directory.CreateDirectory(PhysicsRootPath + "Temp");
|
|
}
|
|
return TempPath;
|
|
}
|
|
#endregion
|
|
|
|
#region 将数据生成为excel文件
|
|
/// <summary>
|
|
/// 将数据生成为excel文件
|
|
/// <param name="info">配置信息</param>
|
|
/// <param name="fileName">文件名</param>
|
|
/// </summary>
|
|
public void WriteWorkbook(List<string> listSheetName, string fileName, DataSet ds)
|
|
{
|
|
List<ISheet> listSheet = new List<ISheet>() ;
|
|
//ISheet sheet = null;
|
|
XSSFWorkbook workbook1 = null;
|
|
HSSFWorkbook workbook2 = null;
|
|
try
|
|
{
|
|
if (fileName.IndexOf(".xlsx") > 0)
|
|
{
|
|
workbook1 = new XSSFWorkbook();
|
|
foreach (var item in listSheetName)
|
|
{
|
|
listSheet.Add(workbook1.CreateSheet(item));
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
workbook2 = new HSSFWorkbook();
|
|
foreach (var item in listSheetName)
|
|
{
|
|
listSheet.Add(workbook2.CreateSheet(item));
|
|
}
|
|
}
|
|
|
|
//写入文件
|
|
using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
|
|
{
|
|
|
|
|
|
IRow dataRow = null;
|
|
ICell contentCell = null;
|
|
|
|
int rowsNum = 0;
|
|
for (int m = 0; m < ds.Tables.Count; m++)
|
|
{
|
|
rowsNum = 0;
|
|
if (ds.Tables[m].Rows.Count == 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
ICellStyle headStyle = workbook1 == null ? workbook2.CreateCellStyle() : workbook1.CreateCellStyle();
|
|
IFont headFont = workbook1 == null ? workbook2.CreateFont() : workbook1.CreateFont();
|
|
|
|
ICellStyle cellStyle = workbook1 == null ? workbook2.CreateCellStyle() : workbook1.CreateCellStyle();
|
|
IFont cellFont = workbook1 == null ? workbook2.CreateFont() : workbook1.CreateFont();
|
|
|
|
#region 写入表头
|
|
IRow headRow = listSheet[m].CreateRow(rowsNum++);
|
|
for (int i = 0; i < ds.Tables[m].Columns.Count; i++)
|
|
{
|
|
ICell cell = headRow.CreateCell(i);
|
|
cell.SetCellValue(ds.Tables[m].Rows[0][i].ToString());
|
|
headStyle.BorderTop = BorderStyle.Thin;
|
|
headStyle.BorderBottom = BorderStyle.Thin;
|
|
headStyle.BorderLeft = BorderStyle.Thin;
|
|
headStyle.BorderRight = BorderStyle.Thin;
|
|
headFont.Boldweight = (short)FontBoldWeight.Bold;
|
|
headStyle.SetFont(headFont);
|
|
cell.CellStyle = headStyle;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 写入行数据
|
|
for (int j = 1; j < ds.Tables[m].Rows.Count; j++)
|
|
{
|
|
dataRow = listSheet[m].CreateRow(rowsNum++);
|
|
for (int i = 0; i < ds.Tables[m].Columns.Count; i++)
|
|
{
|
|
|
|
CellType cellType = CellType.Blank;
|
|
contentCell = dataRow.CreateCell(i, cellType);
|
|
int a = 0;
|
|
if (int.TryParse(ds.Tables[m].Rows[j][i].ToString(), out a))
|
|
{
|
|
contentCell.SetCellValue(a);
|
|
}
|
|
else
|
|
{
|
|
contentCell.SetCellValue(ds.Tables[m].Rows[j][i].ToString());
|
|
}
|
|
|
|
cellStyle.BorderTop = BorderStyle.Thin;
|
|
cellStyle.BorderBottom = BorderStyle.Thin;
|
|
cellStyle.BorderLeft = BorderStyle.Thin;
|
|
cellStyle.BorderRight = BorderStyle.Thin;
|
|
|
|
cellStyle.SetFont(cellFont);
|
|
contentCell.CellStyle = cellStyle;
|
|
}
|
|
|
|
}
|
|
#endregion
|
|
|
|
rowsNum++;
|
|
}
|
|
|
|
if (fileName.IndexOf(".xlsx") > 0)
|
|
{
|
|
workbook1.Write(fs);
|
|
}
|
|
else
|
|
{
|
|
workbook2.Write(fs);
|
|
}
|
|
|
|
fs.Close();
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// 下载文件
|
|
/// </summary>
|
|
/// <param name="filePath"></param>
|
|
/// <param name="fileName"></param>
|
|
public static void FileDownload(HttpResponseBase Response, string filePath, string fileName)
|
|
{
|
|
Response.ClearHeaders();
|
|
Response.Clear();
|
|
Response.Expires = 0;
|
|
Response.Buffer = true;
|
|
Response.AddHeader("Accept-Language", "zh-tw");
|
|
//string name = System.IO.Path.GetFileName(virtualPath + fileName);
|
|
|
|
System.IO.FileStream files = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
|
|
byte[] byteFile = null;
|
|
if (files.Length == 0)
|
|
{
|
|
byteFile = new byte[1];
|
|
}
|
|
else
|
|
{
|
|
byteFile = new byte[files.Length];
|
|
}
|
|
files.Read(byteFile, 0, (int)byteFile.Length);
|
|
files.Close();
|
|
fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);
|
|
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
|
|
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
|
Response.BinaryWrite(byteFile);
|
|
Response.End();
|
|
FileInfo file = new FileInfo(filePath);
|
|
file.Delete();
|
|
}
|
|
}
|
|
}
|
|
|