public class LoadData
{
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="dt">要写入的数据</param>
/// <param name="filePath">要保存的excel路径</param>
/// <param name="fileName">excel文件名</param>
public static bool WriteExcel(DataTable dt, string filePath, string fileName)
{
try
{
if (!string.IsNullOrEmpty(filePath) && null != dt && dt.Rows.Count > 0)
{
NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
NPOI.SS.UserModel.ISheet sheet = book.CreateSheet("Sheet1");
int mid = dt.Columns.Count / 2;
NPOI.SS.UserModel.IRow row0 = sheet.CreateRow(0);
NPOI.SS.UserModel.IRow row1 = sheet.CreateRow(1);
for (int i = 0; i < dt.Columns.Count; i++)
{
if (i == mid)
{
row0.CreateCell(i).SetCellValue(fileName);
}
row1.CreateCell(i).SetCellValue(dt.Columns[i].ColumnName);
}
for (int i = 0; i < dt.Rows.Count; i++)
{
NPOI.SS.UserModel.IRow row2 = sheet.CreateRow(i + 2);
for (int j = 0; j < dt.Columns.Count; j++)
{
row2.CreateCell(j).SetCellValue(Convert.ToString(dt.Rows[i][j]));
}
}
// 写入到客户端
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
book.Write(ms);
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
byte[] data = ms.ToArray();
fs.Write(data, 0, data.Length);
fs.Flush();
}
book = null;
}
}
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 创建pdf
/// </summary>
/// <param name="pdffilepath">pdf文件名</param>
/// <param name="datatable">要写入的数据</param>
/// <param name="doctitle">文档的标题</param>
/// <param name="pageheader">可为空</param>
/// <param name="headerImgPath">可为空</param>
/// <returns></returns>
public static bool WritePDF(string pdffilepath, DataTable datatable, string doctitle, string chinesetitle, string pageheader, string headerImgPath)
{
//new RectangleReadOnly(842F, 595F)横向打印
Document doc = new Document(new RectangleReadOnly(842F, 595F));//创建一个iTextSharp document对象
try
{
if (!Directory.Exists(pdffilepath))//如果日志目录不存在就创建
{
Directory.CreateDirectory(pdffilepath);
}
PdfWriter pdfw = PdfWriter.GetInstance(doc, new FileStream(pdffilepath + "\\" + doctitle + ".pdf", FileMode.Create));//为doc对象创建PdfWriter实例
doc.Open();//打开doc对象
string fontpath = "C:/WINDOWS/Fonts/simfang.ttf"; //System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "Manage\\fonts\\SIMYOU.TTF";
if (!System.IO.File.Exists(fontpath))
{
Log4NetHelper.Error(typeof(LoadData), "缺失字体文件" + fontpath);
return false;
}
BaseFont bfont = BaseFont.CreateFont(fontpath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//使用BaseFont加载unicode字体像简体中文字体
Font fonttitle = new Font(bfont, 16);//创建新的基于BaseFont字体
Font fontheader = new Font(bfont, 12);//创建新的基于BaseFont字体
Font fontcontent = new Font(bfont, 10);//创建新的基于BaseFont字体
Font normalFont = new Font(bfont, 10);//创建新的基于BaseFont字体
if (pageheader.Length > 0)
{
doc.Add(new Paragraph(new Phrase("")));//Paragraph报表中的文本
}
if (headerImgPath != "")
{
Image image = Image.GetInstance(headerImgPath);
image.SetAbsolutePosition(0, 0);
image.Alignment = Image.ALIGN_UNDEFINED;
image.ScaleAbsolute(image.Width, image.Height);
doc.Add(image);//new PdfPCell(image, true)
}
if (doctitle.Length > 0)
{
Paragraph pgtitle = new Paragraph(new Phrase(chinesetitle, fonttitle));
PdfPCell pdfcell = new PdfPCell(pgtitle);
pdfcell.HorizontalAlignment = 1;
pdfcell.VerticalAlignment = 1;
pdfcell.Border = 0;
PdfPTable pdftb = new PdfPTable(1);
pdftb.AddCell(pdfcell);
doc.Add(pdftb);//写标题
//设置一个空白换行
float normalLineHeight = 25f;
Paragraph pBlank = new Paragraph(" ", normalFont);
pBlank.Leading = normalLineHeight;
doc.Add(pBlank);
}
if (datatable.Rows.Count == 0)
{
return false;
}
int cols = datatable.Columns.Count;
int rows = datatable.Rows.Count;
PdfPTable ptable = new PdfPTable(cols);//创建pdf表对象
ptable.WidthPercentage = 100;//宽度的百分比
PdfPCell _pdfpcell;//表单元格
//写表头
for (int c = 0; c < cols; c++)
{
Phrase _phrase = new Phrase((datatable.Columns[c].Caption != null && datatable.Columns[c].Caption.Length > 0) ? datatable.Columns[c].Caption : datatable.Columns[c].ColumnName, fontheader);
_pdfpcell = new PdfPCell(_phrase);
_pdfpcell.PaddingBottom = 5f;
_pdfpcell.PaddingTop = 5f;
_pdfpcell.PaddingLeft = 8f;
_pdfpcell.PaddingRight = 8f;
_pdfpcell.HorizontalAlignment = 1;
_pdfpcell.VerticalAlignment = 1;
ptable.AddCell(_pdfpcell);
}
//写表体
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
_pdfpcell = new PdfPCell(new Phrase((datatable.Rows[i][j] != null) ? datatable.Rows[i][j].ToString() : "", fontcontent));
_pdfpcell.PaddingBottom = 5f;
_pdfpcell.PaddingTop = 5f;
_pdfpcell.PaddingLeft = 8f;
_pdfpcell.PaddingRight = 8f;
_pdfpcell.HorizontalAlignment = 1;
_pdfpcell.VerticalAlignment = 1;
ptable.AddCell(_pdfpcell);
}
}
return doc.Add(ptable);
}
catch (Exception ex)
{
return false;
}
finally
{
doc.Close();
}
}
}