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(" ","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;
}