Using JavaScript to Open Excel and Word Files in HTML

by Yang Yang on January 5, 2009

One of the main reasons that you might need this is that Microsoft Excel and Word documents pervade. They are perfect media to store, display and distribute information and ideas.

A rather straightforward approach is to simply link that document up.

<a href="bank/sheet.xls" >some excel file</a>

Another approach is via JavaScript.

<script type="text/javascript">
	function openExcel(strFilePath) {
		var yourSite = "http://www.yoursite.com";
		openExcelDocPath(yourSite + strFilePath, false);
	}
	function openExcelDocPath(strLocation, boolReadOnly) {
		var objExcel;
		objExcel = new ActiveXObject("Excel.Application");
		objExcel.Visible = true;
		objExcel.Workbooks.Open(strLocation, false, boolReadOnly);
	}
</script>

To open a word document with javascript, go with the following code:

<script type="text/javascript">
	function openWord(strFilePath) {
		var yourSite = "http://www.yoursite.com";
		openWordDocPath(yourSite + strFilePath);
	}
	function openWordDocPath(strLocation) {
		var objWord;
		objWord = new ActiveXObject("Word.Application");
		objWord.Visible = true;
		objWord.Documents.Open(strLocation);
	}
</script>
Subscribe to Kavoir: blog feed

You should also read:

{ 17 comments… read them below or add one }