Tag Archive: ActiveX


Export DispForm to Excel

To export an item out of a SharePoint DispForm you can use the following javascript function.

function Export()
{
	var table = $("table[class='ms-formtable']");
	var replacer = new RegExp("<BR>","g");
	var nbspReplacer = new RegExp("&nbsp;","g");		

	var xls = new ActiveXObject("Excel.Application")
	xls.visible = true
	var xlsBook = xls.Workbooks.Add
	countRow = 2;
	countColumn = 1;
	xls.columns("A").columnwidth=40;
	xls.columns("B").columnwidth=60;
	xls.columns("B").WrapText = true;
	xls.columns("A").verticalAlignment = -4160;
	xls.columns("B").verticalAlignment = -4160;
	xls.Cells( 1, 1).interior.colorindex=44;
	xls.Cells( 1, 2).interior.colorindex=44;
	xls.Cells( 1, 2).Value = "GG-Zwischenfall";
	xls.Cells( 1, 2).font.name="Arial";
  	xls.Cells( 1, 2).font.size=10;
  	xls.Cells( 1, 2).font.bold=true;
  	xls.Cells( 1, 2).borders(10).LineStyle = 1;
  	xls.Cells( 1, 2).borders(10).Weight = -4138;
        xls.Cells( 1, 2).borders(10).ColorIndex = -4105;
        xls.Cells( 1, 1).borders(7).LineStyle = 1;
  	xls.Cells( 1, 1).borders(7).Weight = -4138;
        xls.Cells( 1, 1).borders(7).ColorIndex = -4105;
        xls.Cells( 1, 2).borders(8).LineStyle = 1;
  	xls.Cells( 1, 2).borders(8).Weight = -4138;
        xls.Cells( 1, 2).borders(8).ColorIndex = -4105;
        xls.Cells( 1, 1).borders(8).LineStyle = 1;
  	xls.Cells( 1, 1).borders(8).Weight = -4138;
        xls.Cells( 1, 1).borders(8).ColorIndex = -4105;

        xlsBook.activeSheet.pageSetup.Zoom = false;
        xlsBook.activeSheet.pageSetup.FitToPagesWide = 1;
        xlsBook.activeSheet.pageSetup.FitToPagesTall = 1;
	var aufgestellt = false;
	$(table).children('TBODY').children('TR').each(function() {
  		var tr = $(this);
  		tr.find('TD').each(function() {
  			//xls.Cells( countRow, countColumn).Value = unescape($(this).text());
  			if (countColumn < 3)
  			{
  			var html = $(this).html();
  			var text = $(this).text();
  			if (html.indexOf("<DIV>") != -1 && text != "" && html.indexOf("<BR>") != -1)
  			{
  				var htmlValue = $(this).children('DIV').html();
  				var textValue = $(this).children('DIV').text();
  				//alert(textValue);
  				if (htmlValue != "" && htmlValue.indexOf("<DIV>") == -1)
  				{
  					//alert(htmlValue);
  					htmlValue = htmlValue.replace(replacer,"\n");
  					htmlValue = htmlValue.replace(nbspReplacer," ");
  					xls.Cells( countRow, countColumn).Value = unescape(htmlValue);
  				}
  				else
  				{
  					var divTextValue = "";
  					$(this).children('DIV').children('DIV').each(function() {
  						//alert ($(this).text());
  						divTextValue = divTextValue + $(this).html().replace(replacer,"\n");
   					});
   					divTextValue = divTextValue.replace(nbspReplacer," ");
  					xls.Cells( countRow, countColumn).Value = unescape(divTextValue);
  				}
  			}
  			else
  			{
  				if (html.indexOf("<DIV>") != -1 && text != "" && html.indexOf("<BR>") == -1)
  				{
  					if ($(this).children('DIV').find("DIV").length > 0)
  					{
  						var divTextValue = "";
  						$(this).children('DIV').children('DIV').each(function() {
  							//alert ($(this).text());
  							divTextValue = divTextValue + $(this).text() + "\n";
   						});
   						divTextValue = divTextValue.replace(new RegExp("\n" + '$'), '');
   						divTextValue = divTextValue.replace(nbspReplacer," ");
  						xls.Cells( countRow, countColumn).Value = unescape(divTextValue);
  					}
  					else
  					{
  						xls.Cells( countRow, countColumn).Value = unescape($(this).text());
  					}
  				}
  				else
  				{
  					xls.Cells( countRow, countColumn).Value = unescape($(this).text());
  				}
  			}
  			//xls.Cells( countRow, countColumn).Value = $(this).html();
  			if (countColumn == 1)
  			{
  				xls.Cells( countRow, countColumn).font.bold="true";
  				xls.Cells( countRow, countColumn).borders(7).LineStyle = 1;
  				xls.Cells( countRow, countColumn).borders(7).Weight = -4138;
        		        xls.Cells( countRow, countColumn).borders(7).ColorIndex = -4105;
  			}
  			else
  			{
  				xls.Cells( countRow, countColumn).interior.colorindex=19;
  				xls.Cells( countRow, countColumn).borders(10).LineStyle = 1;
  				xls.Cells( countRow, countColumn).borders(10).Weight = -4138;
        		        xls.Cells( countRow, countColumn).borders(10).ColorIndex = -4105;
  			}
  			xls.Cells( countRow, countColumn).borders(8).LineStyle = 1;
  			xls.Cells( countRow, countColumn).borders(8).Weight = -4138;
        	        xls.Cells( countRow, countColumn).borders(8).ColorIndex = 15;
  			xls.Cells( countRow, countColumn).borders(9).LineStyle = 1;
  			xls.Cells( countRow, countColumn).borders(9).Weight = -4138;
        	        xls.Cells( countRow, countColumn).borders(9).ColorIndex = 15;
  			xls.Cells( countRow, countColumn).font.name="Tahoma";
  			xls.Cells( countRow, countColumn).font.size=8;
  			}
  			countColumn++;
    	});

    countRow++;
    countColumn = 1;
	});	

	$("td[class='ms-descriptiontext']").each(function() {
		xls.Cells( countRow, 1).Value = $(this).text();
		xls.Cells( countRow, 1).font.name="Tahoma";
  		xls.Cells( countRow, 1).font.size=8;
  		xls.Cells( countRow, 2).borders(10).LineStyle = 1;
  		xls.Cells( countRow, 2).borders(10).Weight = -4138;
                xls.Cells( countRow, 2).borders(10).ColorIndex = -4105;
                xls.Cells( countRow, 1).borders(7).LineStyle = 1;
  		xls.Cells( countRow, 1).borders(7).Weight = -4138;
                xls.Cells( countRow, 1).borders(7).ColorIndex = -4105;

		countRow++;
	});
	xls.Cells( countRow, 2).borders(10).LineStyle = 1;
  	xls.Cells( countRow, 2).borders(10).Weight = -4138;
        xls.Cells( countRow, 2).borders(10).ColorIndex = -4105;
        xls.Cells( countRow, 1).borders(9).LineStyle = 1;
  	xls.Cells( countRow, 1).borders(9).Weight = -4138;
        xls.Cells( countRow, 1).borders(9).ColorIndex = -4105;
        xls.Cells( countRow, 2).borders(9).LineStyle = 1;
  	xls.Cells( countRow, 2).borders(9).Weight = -4138;
        xls.Cells( countRow, 2).borders(9).ColorIndex = -4105;
        xls.Cells( countRow, 1).borders(7).LineStyle = 1;
  	xls.Cells( countRow, 1).borders(7).Weight = -4138;
        xls.Cells( countRow, 1).borders(7).ColorIndex = -4105;
	xls.Cells( countRow-1, 1).Value = "";
	//xls.columns.autofit;
}

This javascript function can be used to export data from a SharePoint NewForm to Excel. People Picker fields and Date fields are not covered by this function.

function Export()
{
	var table = $("table[class='ms-formtable']");
	var replacer = new RegExp("<BR>","g");
	var nbspReplacer = new RegExp("&nbsp;","g");
	var divReplacer = new RegExp("<div>","g");
	var divEndReplacer = new RegExp("</div>","g");
	var divExtClassReplacer = new RegExp("<div>\n", "g");
	var fieldTitle = "";
	var fieldValue = "";	

	var xls = new ActiveXObject("Excel.Application")
	xls.visible = true
	var xlsBook = xls.Workbooks.Add
	countRow = 2;
	countColumn = 1;
	xls.columns("A").columnwidth=40;
	xls.columns("B").columnwidth=60;
	xls.columns("B").WrapText = true;
	xls.columns("A").verticalAlignment = -4160;
	xls.columns("B").verticalAlignment = -4160;
	xls.columns("B").horizontalAlignment = -4131;
	xls.Cells( 1, 1).interior.colorindex=44;
	xls.Cells( 1, 2).interior.colorindex=44;
	xls.Cells( 1, 2).Value = "GG-Zwischenfall";
	xls.Cells( 1, 2).font.name="Arial";
  	xls.Cells( 1, 2).font.size=10;
  	xls.Cells( 1, 2).font.bold=true;
  	xls.Cells( 1, 2).borders(10).LineStyle = 1;
  	xls.Cells( 1, 2).borders(10).Weight = -4138;
        xls.Cells( 1, 2).borders(10).ColorIndex = -4105;
        xls.Cells( 1, 1).borders(7).LineStyle = 1;
  	xls.Cells( 1, 1).borders(7).Weight = -4138;
        xls.Cells( 1, 1).borders(7).ColorIndex = -4105;
        xls.Cells( 1, 2).borders(8).LineStyle = 1;
  	xls.Cells( 1, 2).borders(8).Weight = -4138;
        xls.Cells( 1, 2).borders(8).ColorIndex = -4105;
        xls.Cells( 1, 1).borders(8).LineStyle = 1;
  	xls.Cells( 1, 1).borders(8).Weight = -4138;
        xls.Cells( 1, 1).borders(8).ColorIndex = -4105;
        xlsBook.activeSheet.pageSetup.Zoom = false;
        xlsBook.activeSheet.pageSetup.FitToPagesWide = 1;
        xlsBook.activeSheet.pageSetup.FitToPagesTall = 1;

	$(table).children('TBODY').children('TR').each(function() {
  		var tr = $(this);
  		tr.children('TD').each(function() {
  			if (countColumn < 3)
  			{
  			if (countColumn == 1)
  			{
  				fieldTitle = unescape($(this).text()).replace(" *","");
  				xls.Cells( countRow, countColumn).Value = unescape(fieldTitle);
  				xls.Cells( countRow, countColumn).font.bold="true";
  				xls.Cells( countRow, countColumn).borders(7).LineStyle = 1;
  				xls.Cells( countRow, countColumn).borders(7).Weight = -4138;
        		        xls.Cells( countRow, countColumn).borders(7).ColorIndex = -4105;
  			}
  			if (countColumn == 2)
  			{
  				fieldValue = $("input[title = "+fieldTitle+"]").val();
  				if (fieldValue == null)
  				{
  					fieldValue = $(this).find("input[id $= 'TextField']").val();
  				}
  				if ($(this).find("select[id$='DateHours']").length > 0 && $(this).find("select[id$='DateMinutes']").length > 0)
  				{
  					fieldValue = fieldValue + " " + $(this).find("select[id$='DateHours']").children(":selected").text() + $(this).find("select[id$='DateMinutes']").children(":selected").text();
  				}

  				if (fieldValue == null)
  				{
  					fieldValue = $(this).find("textarea").val();
  					if ($(this).find("textarea:hidden").length > 0)
  					{
  					    //alert("textarea");
  						$(this).children("SPAN").children("SPAN").children("DIV").children("IFRAME").contents().find("DIV").each(function() {
  							fieldValue = fieldValue + $(this).text() + "\n";
   						});
   						if (fieldValue != null)
   						{
   							fieldValue = fieldValue.replace(new RegExp("\n" + '$'), '');
   							fieldValue = fieldValue.replace("undefined","");
   						}
  					}

  					if (fieldValue != null && fieldValue.indexOf('<div>') != -1)
  					{
  						fieldValue = fieldValue.replace("undefined","");
  						fieldValue = unescape(fieldValue.replace(divReplacer, ""));
  						fieldValue = unescape(fieldValue.replace(divEndReplacer, ""));
  						fieldValue = unescape(fieldValue.replace(divExtClassReplacer, ""));
  						fieldValue = unescape(fieldValue.replace(nbspReplacer, ""));
  					}
  				}
  				if (fieldValue == null)
  				{
  					$(this).find('input:checkbox:checked').each(function(index) {
  						if ($(this).parent().parent().parent().next("TR").find("input[title^="+fieldTitle+"]").length > 0)
  						{
  							fieldValue = fieldValue + $(this).parent().parent().parent().next("TR").find("input").val();
  						}
  						else
  						{
  							fieldValue = fieldValue + $(this).next("label").text() + "; ";
  						}
  					});
  					if (fieldValue != null)
  					{
  						fieldValue = fieldValue.replace("undefined","");
  						fieldValue = fieldValue.replace(new RegExp("; " + '$'), '');
  						//alert(fieldValue);
  					}
  				}
  				if (fieldValue == null)
  				{
  					if ($(this).find('input:radio:checked').parent().next("label").next("select").length > 0)
  					{
  						fieldValue = $(this).find('input:radio:checked').parent().next("label").next("select").children(":selected").text();
  					}
  					else
  					//if (fieldValue == null || fieldValue == '')
  					{
  						//alert(fieldValue);
  						fieldValue = $(this).find('input:radio:checked').parent().parent().parent().next("TR").find("input").val();
  					}
  				}
  				if (fieldValue == null || fieldValue == '')
  				{
  					fieldValue = $(this).find("select").children(":selected").text();
  				}
  				if ($(this).parent().attr('id') == "idAttachmentsRow" && (fieldValue == null || fieldValue == ''))
  				{
  					fieldValue = $(this).find("TD").first().text();
  				}

  				xls.Cells( countRow, countColumn).Value = unescape(fieldValue);
  				xls.Cells( countRow, countColumn).interior.colorindex=19;
  				xls.Cells( countRow, countColumn).borders(10).LineStyle = 1;
  				xls.Cells( countRow, countColumn).borders(10).Weight = -4138;
        		        xls.Cells( countRow, countColumn).borders(10).ColorIndex = -4105;
  			}
  			xls.Cells( countRow, countColumn).borders(8).LineStyle = 1;
  			xls.Cells( countRow, countColumn).borders(8).Weight = -4138;
        	        xls.Cells( countRow, countColumn).borders(8).ColorIndex = 15;
  			xls.Cells( countRow, countColumn).borders(9).LineStyle = 1;
  			xls.Cells( countRow, countColumn).borders(9).Weight = -4138;
        	        xls.Cells( countRow, countColumn).borders(9).ColorIndex = 15;
  			xls.Cells( countRow, countColumn).font.name="Tahoma";
  			xls.Cells( countRow, countColumn).font.size=8;
  			}
  			countColumn++;
    	});

    countRow++;
    countColumn = 1;
	});	

	$("td[class='ms-descriptiontext']").each(function() {
		xls.Cells( countRow, 1).Value = $(this).text();
		xls.Cells( countRow, 1).font.name="Tahoma";
  		xls.Cells( countRow, 1).font.size=8;
  		xls.Cells( countRow, 2).borders(10).LineStyle = 1;
  		xls.Cells( countRow, 2).borders(10).Weight = -4138;
                xls.Cells( countRow, 2).borders(10).ColorIndex = -4105;
                xls.Cells( countRow, 1).borders(7).LineStyle = 1;
  		xls.Cells( countRow, 1).borders(7).Weight = -4138;
                xls.Cells( countRow, 1).borders(7).ColorIndex = -4105;

		countRow++;
	});
	xls.Cells( countRow, 2).borders(10).LineStyle = 1;
  	xls.Cells( countRow, 2).borders(10).Weight = -4138;
        xls.Cells( countRow, 2).borders(10).ColorIndex = -4105;
        xls.Cells( countRow, 1).borders(9).LineStyle = 1;
  	xls.Cells( countRow, 1).borders(9).Weight = -4138;
        xls.Cells( countRow, 1).borders(9).ColorIndex = -4105;
        xls.Cells( countRow, 2).borders(9).LineStyle = 1;
  	xls.Cells( countRow, 2).borders(9).Weight = -4138;
        xls.Cells( countRow, 2).borders(9).ColorIndex = -4105;
        xls.Cells( countRow, 1).borders(7).LineStyle = 1;
  	xls.Cells( countRow, 1).borders(7).Weight = -4138;
        xls.Cells( countRow, 1).borders(7).ColorIndex = -4105;
	xls.Cells( countRow-1, 1).Value = "";
	//xls.columns.autofit;
}