MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: to XLS


I will describe below two methods to give a name to an Excel worksheet
from Mathematica. The first exports an XMLSS string into an XML file
which can be opened inExcel, the second opens and writes into an Excel
workbook (you must have Excel installed on your system to do this).

If you need more information on this please consult the links at
""; or

Method 1 using Export as "Text" into an "XML" file

Step 1: define the following functions:

xmlCell[data_] := Block[{apo = "\"", tab = "\t"},
      StringJoin[tab, tab, "<Cell><Data ss:Type=", apo, If[
      NumberQ[data], "Number", "String"], apo, ">",
        ToString[data], "</Data></Cell>"]];

xmlRow[rows_] := Block[{nL = "\n", tab = "\t"},
    StringJoin[tab, "<Row>", nL, Map[(xmlCell[#] <> nL) &, rows],
        tab, "</Row>"]]

xmlTable[tbl_] := Block[{nL = "\n"},
      StringJoin["<Table>", nL, Map[(xmlRow[#] <> nL) &, tbl],

xmlWorksheet[name_, tbl_] := Block[{apo = "\"", nL = "\n"},
      StringJoin["<Worksheet ss:Name=", apo,
      ToString[name], apo, ">", nL, xmlTable[tbl], nL,

xmlWorkbook[tbl_] := Block[{apo = "\"", nL = "\n"},
      StringJoin["<?xml version='1.0'?>", nL, "<Workbook
      xmlns=", apo, "urn:schemas-microsoft-com:office:spreadsheet",
" xmlns:ss=", apo, "urn:schemas-
            microsoft-com:office:spreadsheet", apo, ">", nL,
          {{__, {__ ..}} ..}, Map[xmlWorksheet[#[[1]], #[[2]]] &, tbl],
          {__, {{__} ..}}, xmlWorksheet[tbl[[1]], tbl[[2]]],
          {__, {__}}, xmlWorksheet[tbl[[1]], {tbl[[2]]}],
          _, xmlWorksheet["Incorrect Input", {""}]
        nL, "</Workbook>"]];

Step 2: create (test) tables (3-fold nested list, matrix, list which
satisfy the expression matching test in the xmlWorkbook function

tblDim3 = Table[{"Name " <> ToString[k], Table[i + 10j + 100k, {i, 1,
2}, {j, \
1, 2}]}, {k, 1, 2}]
tblDim2 = {"Name 1", Table[i + 10j, {i, 1, 2}, {j, 1, 2}]}
tblDim1 = {"Name 1", Table[i, {i, 1, 2}]}

Step 3: export the tables into an XML file.

dirName=... as you like ...
Export[dirName <> "ForumContributionDim0.xml", xmlWorkbook[{}],
Export[dirName <> "ForumContributionDim1.xml", xmlWorkbook[tblDim1],
Export[dirName <> "ForumContributionDim2.xml", xmlWorkbook[tblDim2],
Export[dirName <> "ForumContributionDim3.xml", xmlWorkbook[tblDim3],

Step 4: open the XML file in Excel

Method 2 using NETLink

An alternative would be to use NETLink Programming. The following
command installs NETLink, creates an Excel application object, opens
Excel, adds a workbook, adds a worksheet, sets the name of the
worksheet and writes some values into a range object.

excel@Workbooks[]@Item[1]@Sheets@Item[1]@Name="My Sheet Name";

I hope this information will help you.

Best regards
Norbert Marxer

  • Prev by Date: Re: Re: Re: Types in Mathematica
  • Next by Date: MATHwire: Information for Mathematica Users
  • Previous by thread: Re: to XLS
  • Next by thread: Time needed for calculation