Javascript to generate excel 1

來源:互聯網
上載者:User

 

using System;using System.Text;namespace CCC.Utility{/// <summary>/// Summary description for JSExcel./// </summary>public class Excel4JavaScript{private string _ReportTemplate_Excel_FileName = null;private int _Used_WorkSheet_MaxIndex = 0;StringBuilder _SB_JavaScript_Text = new StringBuilder();private int _WorkSheetCount = 1; private int _OriginalSheetCount = 1; public Excel4JavaScript(string strFileName){_ReportTemplate_Excel_FileName = strFileName;}public Excel4JavaScript(string strFileName, int originalSheetCount){_WorkSheetCount = originalSheetCount;_OriginalSheetCount = originalSheetCount;_ReportTemplate_Excel_FileName = strFileName;}               public void SetWorkSheetName(int sheetIndex, string strWorkSheetName){_SB_JavaScript_Text.Append("objSheet").Append(sheetIndex).Append(".Name='").Append(strWorkSheetName).Append("';").Append("\r\n");}public void CopyWorkSheet(int sheetIndex, string sheetName){_WorkSheetCount += 1;_SB_JavaScript_Text.Append("objSheet").Append(sheetIndex).Append(".Copy(null, objWorkBook.Worksheets(objWorkBook.Worksheets.count));").Append("\r\n");_SB_JavaScript_Text.Append("objSheet").Append(_WorkSheetCount).Append(" = objWorkBook.Worksheets(").Append(_WorkSheetCount).Append(");").Append("\r\n");_SB_JavaScript_Text.Append("objSheet").Append(_WorkSheetCount).Append(".Name='").Append(sheetName).Append("';").Append("\r\n");}public void HideSheet(int sheetIndex){//_WorkSheetCount -= 1;_SB_JavaScript_Text.Append("objSheet").Append(sheetIndex).Append(".Visible = false;").Append("\r\n");}public void WriteCell( int sheetIndex, int row, int col, string strCellValue){if(_Used_WorkSheet_MaxIndex < sheetIndex){_Used_WorkSheet_MaxIndex = sheetIndex;}_SB_JavaScript_Text.Append("objSheet").Append(sheetIndex).Append(".Cells(").Append(row).Append(",").Append(col).Append(").value = '").Append(strCellValue).Append("';").Append("\r\n");}         public  void WriteComment( string strCellValue, int RowStart, int ColStart, int sheetIndex){WriteCell(sheetIndex,RowStart,ColStart,strCellValue); }        public void DeleteColumn(int col, int sheetIndex){if(_Used_WorkSheet_MaxIndex < sheetIndex){_Used_WorkSheet_MaxIndex = sheetIndex;}_SB_JavaScript_Text.Append("objSheet").Append(sheetIndex).Append(".Columns(").Append(col).Append(").Delete").Append(";").Append("\r\n"); }public void InsertRow(int row, int sheetIndex){_SB_JavaScript_Text.Append("objSheet").Append(sheetIndex).Append(".Rows(").Append(row).Append(").Insert();").Append("\r\n");}public void DeleteRows(int row, int sheetIndex){if (_Used_WorkSheet_MaxIndex < sheetIndex){_Used_WorkSheet_MaxIndex = sheetIndex;}_SB_JavaScript_Text.Append("objSheet").Append(sheetIndex).Append(".Rows(").Append(row).Append(").Delete").Append(";").Append("\r\n"); }public string ToExcelString()    { StringBuilder sbJsText = new StringBuilder();          sbJsText.Append("<script language='JavaScript'>").Append("\r\n");sbJsText.Append("var idTmr = null;").Append("\r\n");sbJsText.Append("function openExcel() {").Append("\r\n");for( int i = 1; i <= _WorkSheetCount; i++){sbJsText.Append("  var objSheet" + i.ToString() + ";\r\n");}sbJsText.Append("  var objExcel = new ActiveXObject('Excel.Application');").Append("\r\n");            sbJsText.Append("  try {").Append("\r\n");sbJsText.Append("    objExcel.DisplayAlerts = false;").Append("\r\n");           sbJsText.Append("    var objWorkBook = objExcel.Workbooks.Open('").Append(this._ReportTemplate_Excel_FileName).Append("');").Append("\r\n");            for( int i = 1; i <= _OriginalSheetCount; i++){sbJsText.Append("    objSheet" + i.ToString() + " = objWorkBook.Worksheets(" + i.ToString() + ");").Append("\r\n");}            sbJsText.Append(this._SB_JavaScript_Text.ToString());sbJsText.Append("    objWorkBook.Worksheets(1).Activate();").Append("\r\n");sbJsText.Append("    objExcel.Visible = true;").Append("\r\n"); sbJsText.Append("  } catch (ex) {").Append("\r\n").Append("\r\n");sbJsText.Append("  } finally {").Append("\r\n");            sbJsText.Append("  }").Append("\r\n");sbJsText.Append("}").Append("\r\n").Append("\r\n");            sbJsText.Append("openExcel();").Append("\r\n");sbJsText.Append("</script> ").Append("\r\n"); return sbJsText.ToString();}}}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.