Example #1
0
        static void Main(string[] args)
        {
            HSSFWorkbook workbook;
            //the excel file to convert
            string fileName = "19599-1.xls";

            fileName = Path.Combine(Environment.CurrentDirectory, fileName);
            workbook = ExcelToHtmlUtils.LoadXls(fileName);


            ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter();

            //set output parameter
            excelToHtmlConverter.OutputColumnHeaders = false;
            excelToHtmlConverter.OutputHiddenColumns = true;
            excelToHtmlConverter.OutputHiddenRows    = true;
            excelToHtmlConverter.OutputLeadingSpacesAsNonBreaking = false;
            excelToHtmlConverter.OutputRowNumbers = true;
            excelToHtmlConverter.UseDivsToSpan    = true;

            //process the excel file
            excelToHtmlConverter.ProcessWorkbook(workbook);

            //output the html file
            excelToHtmlConverter.Document.Save(Path.ChangeExtension(fileName, "html"));
        }
        public void Main()
        {
            HSSFWorkbook workbook;
            //the excel file to convert
            string fileName = "19599-1.xls";

            fileName = Path.Combine(Directory.GetCurrentDirectory(), fileName);
            workbook = ExcelToHtmlUtils.LoadXls(fileName);

            ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter();

            //set output parameter
            excelToHtmlConverter.OutputColumnHeaders = false;
            excelToHtmlConverter.OutputHiddenColumns = true;
            excelToHtmlConverter.OutputHiddenRows    = true;
            excelToHtmlConverter.OutputLeadingSpacesAsNonBreaking = false;
            excelToHtmlConverter.OutputRowNumbers = true;
            excelToHtmlConverter.UseDivsToSpan    = true;

            //process the excel file
            excelToHtmlConverter.ProcessWorkbook(workbook);

            //output the html file
            using (var fs = new FileStream(Path.ChangeExtension(fileName, "html"), FileMode.Create))
            {
                excelToHtmlConverter.Document.Save(fs);
            }
        }
        private void Test(string fileName)
        {
            HSSFWorkbook workbook;

            workbook = ExcelToHtmlUtils.LoadXls(fileName);
            ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter();

            excelToHtmlConverter.ProcessWorkbook(workbook);
            excelToHtmlConverter.Document.Save(Path.ChangeExtension(fileName, "html"));;
        }
Example #4
0
 public static string Excel2html3(string strFile)
 {
     //资源锁,为了保证该资源只能存在一个
     lock (lockObj)
     {
         //文档转换成功,利用NPOI将Excel转为html
         string html = "";
         try
         {
             HSSFWorkbook         workbook             = ExcelToHtmlUtils.LoadXls(strFile);
             ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter();
             excelToHtmlConverter.OutputColumnHeaders = false;
             excelToHtmlConverter.OutputHiddenColumns = false;
             excelToHtmlConverter.OutputHiddenRows    = false;
             excelToHtmlConverter.OutputLeadingSpacesAsNonBreaking = false;
             excelToHtmlConverter.OutputRowNumbers = false;
             excelToHtmlConverter.UseDivsToSpan    = false;
             // 处理的Excel文件
             excelToHtmlConverter.ProcessWorkbook(workbook);
             //添加表格样式
             excelToHtmlConverter.Document.InnerXml =
                 excelToHtmlConverter.Document.InnerXml.Insert(
                     excelToHtmlConverter.Document.InnerXml.IndexOf("<head>", 0) + 6,
                     @"<style>table, td, th{border:1px solid green;}th{background-color:green;color:white;}</style>"
                     );
             //方法一
             html  = excelToHtmlConverter.Document.InnerXml;
             html += @"<style>tr:nth-of-type(1){background: #fcf;white-space: no-wrap;}</style>";
             html += @"<script>var col = document.getElementsByTagName('col');for(i=0;i<col.length;i++){col[i].setAttribute('width','220');}</script>";
         }
         catch
         {
             html = "";
         }
         return(html);
     }
 }
Example #5
0
 public static string Excel2html(HttpRequestBase request, string strFile)
 {
     //资源锁,为了保证该资源只能存在一个
     lock (lockObj)
     {
         string temp = request.MapPath("/Uploads/Temp/");
         temp = temp.Replace("\\", "/");
         if (!temp.EndsWith("/"))
         {
             temp += "/";
         }
         temp = temp + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
         temp = temp.Replace("/", "\\");
         //将strFile另存为xls  可能为旧版的xls  也可能是xlsx  统一转成03 xp版的xls文件
         try
         {
             object oMissing = Type.Missing;
             var    app      = new Microsoft.Office.Interop.Excel.Application();
             var    wb       = app.Workbooks.Open(strFile, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
             wb.CheckCompatibility    = false;
             app.DisplayAlerts        = false;
             wb.DoNotPromptForConvert = true;
             wb.SaveAs(temp, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
             app.Quit();
             //垃圾回收
             //GC.Collect();
             //System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
             //wb = null;
             //GC.Collect();
             //依据时间杀灭进程
             System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("EXCEL");
             foreach (System.Diagnostics.Process p in process)
             {
                 LogHelper.WriteLog("正在关闭Excel");
                 p.Kill();
             }
             GC.Collect();
         }
         //转换失败
         catch (Exception ex)
         {
             LogHelper.WriteLog("1");
             LogHelper.WriteLog(ex.Message);
             try { File.Delete(temp); } catch { }
             return("");
         }
         //文档转换成功,利用NPOI将Excel转为html
         string html = "";
         try
         {
             HSSFWorkbook         workbook             = ExcelToHtmlUtils.LoadXls(temp);
             ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter();
             excelToHtmlConverter.OutputColumnHeaders = false;
             excelToHtmlConverter.OutputHiddenColumns = false;
             excelToHtmlConverter.OutputHiddenRows    = false;
             excelToHtmlConverter.OutputLeadingSpacesAsNonBreaking = false;
             excelToHtmlConverter.OutputRowNumbers = false;
             excelToHtmlConverter.UseDivsToSpan    = false;
             //excelToHtmlConverter.ProcessWorkbook(workbook);
             // 处理的Excel文件
             excelToHtmlConverter.ProcessWorkbook(workbook);
             //添加表格样式
             excelToHtmlConverter.Document.InnerXml =
                 excelToHtmlConverter.Document.InnerXml.Insert(
                     excelToHtmlConverter.Document.InnerXml.IndexOf("<head>", 0) + 6,
                     @"<style>table, td, th{border:1px solid green;}th{background-color:green;color:white;}</style>"
                     );
             //方法一
             html = excelToHtmlConverter.Document.InnerXml;
         }
         catch (Exception exp)
         {
             LogHelper.WriteLog("2");
             LogHelper.WriteLog(exp.Message);
             html = "";
         }
         finally
         {
             //没问题,最后将临时文件删除
             try { File.Delete(temp); } catch { }
         }
         return(html);
     }
 }