如何将Sharepoint List项目ID发送到Excel文件?

时间:2011-01-04 15:03:25

标签: sharepoint excel

我有一个Sharepoint(2007)列表,其中包含一些项目。当我点击其中一个项目时,它将打开一个包含大量宏的Excel(2003)文件。我需要获取此(Sharepoint)项的ID并将其发送到我的Excel文件的单元格...然后将执行一个宏并获取此ID所需的所有数据。

如何将商品的ID发送到我的Excel文件?

有什么想法吗? 感谢

2 个答案:

答案 0 :(得分:0)

我曾经将DataTable写入新的Excel文件中。因此,您可以继续将函数参数从DataTable更改为SPList / SPLisItem,并写入现有文件(我的当前实现每次都写入一个新的Excel文件,我执行此函数)。另外,请确保为Excel(COM)对象添加引用,例如: Microsoft Excel 12.0对象库等如果您需要更多帮助,请通知我。

  public void excelgenerate(DataSet ds)
  {

        Microsoft.Office.Interop.Excel.Application oAppln;
        //declaring work book
        Microsoft.Office.Interop.Excel.Workbook oWorkBook;
        //declaring worksheet
        Microsoft.Office.Interop.Excel.Worksheet oWorkSheet;
        oAppln = new Microsoft.Office.Interop.Excel.Application();
        oWorkBook = (Microsoft.Office.Interop.Excel.Workbook)(oAppln.Workbooks.Add(true));
        Microsoft.Office.Interop.Excel.Range wRange;
        foreach (DataTable table in ds.Tables)
        {
            oWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)(oWorkBook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing));
            oWorkSheet.Name = table.TableName;
            oWorkSheet.Activate();
            DataRow dr = table.Rows[0];                
            string path = dr["Path"].ToString();
            if (path.Length > 0)
            {
                string[] mylist = path.Split('\\');
                var features = Array.FindLastIndex(mylist, str => str.Equals("Features"));
                string stringmine = "Type ---> " + mylist[4]
                   + "/" + mylist[5]
                   + "     Project Name ---> " + mylist[6]
                   + "     Feature Name ---> " + mylist[features + 1];
                oWorkSheet.Cells[1, 1] = stringmine;
                Microsoft.Office.Interop.Excel.Range colrange = oWorkSheet.get_Range(oWorkSheet.Cells[1, 1], oWorkSheet.Cells[1, 8]);
                colrange.Merge(true);

            }


            int ColumnIndex = 0;

            foreach (DataColumn col in table.Columns)
            {
                ColumnIndex++;
                oWorkSheet.Cells[2, ColumnIndex] = col.ColumnName;
                wRange = (Microsoft.Office.Interop.Excel.Range)oWorkSheet.Cells[2, ColumnIndex];
                wRange.Font.Bold = true;
            }
            int rowIndex = 1;
            foreach (DataRow row in table.Rows)
            {
                rowIndex++;
                ColumnIndex = 0;
                foreach (DataColumn col in table.Columns)
                {
                    ColumnIndex++;
                    oWorkSheet.Cells[rowIndex + 1, ColumnIndex] = row[col.ColumnName].ToString();
                }
            }
            oWorkSheet.Columns.AutoFit();
            oWorkSheet.Rows.AutoFit();

        }         

        string fileName = System.Guid.NewGuid().ToString().Replace("-", "") + ".xls";
        Console.WriteLine("Number of sheets written : " + oWorkBook.Worksheets.Count);
        oWorkBook.SaveAs(fileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, null, null, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, false, false, null, null, null);
        oWorkBook.Close(null, null, null);
        oAppln.Quit();

    }

为了使用C#ASP.NET和SharePoint执行宏,我建议你使用这个article

希望它会回答你的问题!

答案 1 :(得分:0)

除非有理由无法将SharePoint列表数据直接链接到工作表并将宏带入该电子表格,否则我认为以下步骤可以满足您的需求。这似乎太简单了......必然有一个原因,这对你正在尝试做的事情不起作用。无论如何,以下是使这项工作的步骤:

1)确保SharePoint列表实际上具有强制唯一值的索引列。您可以通过查看文档库设置来检查这一点。请确保列列表下方有一个索引列。如果没有,可以通过选择"创建新列"来创建一个。操作,选择您的数据类型,并确保选择单词"强制执行唯一值"。

2)使用" export to excel"将库导出到excel;库的主页面菜单中的选项。这将默认建立一个数据链接,并将excel查询文件存储在您计算机上的默认位置,您可以通过转到数据选项卡并选择" connections"来发现该位置。

3)将宏复制到链接到数据源的电子表格中,并调整宏中的引用以从SharePoint列表中提取所需的信息。

希望这会有所帮助。