using NPOI.HPSF;
using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
using System.IO;
private void writeToExcel(HttpResponse res, int sheetCount, DataTable dateTable)
{
string orgfilepath = "D:\\iis folder\\web\\files\\templates\\STORAGE_RECEIVE\\HEARDRECEIVE_temp.xls"; //获得模版的路径
string tarfilepath = orgfilepath.Replace("HEARD", "TEMP" + DateTime.Now.ToString("mmddyyssffff")); //要保存的新的文件名
if (!File.Exists(orgfilepath))
{
return;
}
FileStream file = new FileStream(orgfilepath, FileMode.Open, FileAccess.Read);
hssfworkbook = new HSSFWorkbook(file);
Microsoft.Office.Interop.Excel.Application xApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook xBook;
object oMiss = Missing.Value;
System.IO.File.Copy(orgfilepath, tarfilepath, false);
System.Globalization.CultureInfo currentCultureInfo = System.Globalization.CultureInfo.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
xBook = xApp.Workbooks.Open(tarfilepath, null, oMiss, oMiss, oMiss, oMiss, oMiss, oMiss, oMiss, oMiss, oMiss, oMiss, oMiss, oMiss, oMiss);
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)xBook.Worksheets[1];
for (int j = 0; j < sheetCount - 1; j++) //设置copy几个sheet
{
sheet.Copy(sheet, Type.Missing);
}
xBook.Save();
xApp.Workbooks.Close();
xApp.Quit();
FileStream newfile = new FileStream(tarfilepath, FileMode.Open, FileAccess.Read);
hssfworkbook = new HSSFWorkbook(newfile);
for (int i = 0; i < myTable.Rows.Count; i++)
{
InputTOExcel(); //这个方法往模版里写内容
}
MemoryStream msfile = new MemoryStream();
hssfworkbook.Write(msfile);
System.IO.File.WriteAllBytes(tarfilepath, msfile.ToArray());
string pdfFile = ComMethod.MadePDF(tarfilepath);
byte[] datapdf = System.IO.File.ReadAllBytes(pdfFile);
//删除模版
//System.IO.DirectoryInfo di = new DirectoryInfo("D:\\iis folder\\web\\files\\templates\\STORAGE_RECEIVE");
//FileInfo[] fis = di.GetFiles("TEMP*.*");
//for (int i = 0; i < fis.Length; i++)
//{
// File.Delete(fis[i].FullName);
//}
if (res != null)
{
res.ContentType = "application/octet-stream";
res.AddHeader("Content-Disposition", "attachment;filename=" + "receive_" + DateTime.Now.ToString("yyyyMMdd") + ".pdf");
res.TransmitFile(pdfFile);
//res.BinaryWrite(datapdf);
res.End();
}
}
///
/// 制作PDF文件
///
/// 转换的xls文件
public static string MadePDF(string TargetXlsFile)
{
object oMiss = Missing.Value;
Microsoft.Office.Interop.Excel.Application xApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.XlFixedFormatType excelType = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;
Microsoft.Office.Interop.Excel.Workbook xBookOK = xApp.Workbooks.Open(TargetXlsFile, null, oMiss, oMiss, oMiss, oMiss, oMiss, oMiss, oMiss, oMiss, oMiss, oMiss, oMiss, oMiss, oMiss);
string pdfFile = TargetXlsFile.Replace(".xls", ".pdf");
xBookOK.ExportAsFixedFormat(excelType, pdfFile, XlFixedFormatQuality.xlQualityStandard, true, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
xApp.Workbooks.Close();
xApp.Quit();
return pdfFile;
}