0

昨天我安装了 C# 3.5,今天在这里阅读“使用 Google Data API 使用 C# 访问 Google 电子表格”的主题时,我决定尝试一下。

所以我决定写一个例子,即:1)获取第一个电子表格的第一个工作表的数据馈送,该名称包含用户插入的字符串段 2)用数据填充网格

好吧,我必须分开,用电子表格值数据填充二维数组或列表列表。我一直在寻找一个控件来将数据放入并动态生成它,因为我事先不知道工作表的行或列。

我的直觉告诉我,我可以用 Dataset 更改 List>,然后开始尝试 IntelliSense 向我抛出的不同控件。我知道我应该先读一本书,但我迫不及待地不先尝试。

Xaml

<DockPanel Background="LightSteelBlue">

    <!-- Standard input -->
    <DockPanel DockPanel.Dock="Top" Margin="5">
        <TextBlock Margin="10,0,5,0" VerticalAlignment="Center">Username:</TextBlock>
        <TextBox x:Name="username" Width="120" Padding="5,0,5,0" Text="username@gmail.com" />
        <TextBlock Margin="10,0,5,0" VerticalAlignment="Center">Password:</TextBlock>
        <TextBox x:Name="password" Width="120" Padding="5,0,5,0" Text="password" />
        <TextBlock Margin="10,0,5,0" VerticalAlignment="Center">Workbook:</TextBlock>
        <TextBox x:Name="workbookname" Width="120" Padding="5,0,5,0" Text="name" />
        <Button x:Name="getData" Margin="5,0,0,0" Padding="5,0,5,0" Content="Get _Data" Click="getData_Click" />
    </DockPanel>

    <!-- Grid display -->
    <ListBox Name="lb2"/>

</DockPanel>

和 c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Collections;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Spreadsheets;

namespace getGoogleDocsSpreadsheetData
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private bool RunSample(
            String g_username,
            String g_password,
            String g_spreadsheetname
        ){
            SpreadsheetsService service = new SpreadsheetsService("Margus-stackoverflow_example-1");
            service.setUserCredentials(g_username, g_password);
            CellFeed cfeed = null;

            try {
                //get CellFeed of first Worksheet of first Spreadsheet, that contains _spreadsheetname
                cfeed = ((CellFeed)service.Query(new CellQuery(
                    ((WorksheetEntry)(service.Query(new WorksheetQuery(
                        ((SpreadsheetEntry)(
                            from fe in service.Query(new SpreadsheetQuery()).Entries
                            where fe.Title.Text.Contains(g_spreadsheetname)
                            select fe
                        ).First()).Links.FindService(
                            GDataSpreadsheetsNameTable.WorksheetRel,
                            null
                        ).HRef.ToString())).Entries.First())
                    ).Links.FindService(
                        GDataSpreadsheetsNameTable.CellRel,
                        null
                    ).HRef.ToString()))
                );

                /* get data to array
                string[,] values = new string
                    [(from sr in cfeed.Entries select ((CellEntry)sr).Cell.Row).Max()+1
                    ,(from sr in cfeed.Entries select ((CellEntry)sr).Cell.Column).Max()+1];

                foreach (CellEntry curCell in cfeed.Entries)
                    values[curCell.Cell.Row,curCell.Cell.Column] = curCell.Cell.Value;
                 */

                /* get data to list of list of string */
                List<List<string>> lists = new List<List<string>>();

                for (int i = 0; i < 1+ (
                    from sr in cfeed.Entries 
                    select ((CellEntry)sr).Cell.Row).Max(); i++
                ){
                    List<string> x = new List<string>();
                    for (int j = 0; j < 1+ (
                        from sr in cfeed.Entries 
                        select ((CellEntry)sr).Cell.Column).Max(); j++
                    )
                    x.Add(default(string));

                    lists.Add(x);
                }

                foreach (CellEntry curCell in cfeed.Entries)
                    (lists[(int)curCell.Cell.Row])[(int)curCell.Cell.Column] = curCell.Cell.Value;

                //fill datagrid
                this.lb2.ItemsSource = lists;              
            } catch (Exception e){
                System.Console.WriteLine("I exeeded failing!\n" + e.StackTrace);
                return false;
            }

            return true;
        }

        private void getData_Click(object sender, RoutedEventArgs e)
        {
            RunSample(
                this.username.Text,
                this.password.Text,
                this.workbookname.Text);
        }
    }
}
4

1 回答 1

0

我在 WPFToolkit 中使用了这个:

<my:DataGrid Margin="0,29,0,0" Name="dataGridView" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" AlternatingRowBackground="AliceBlue" ItemsSource="{Binding Path=.}" CanUserResizeRows="False" ClipboardCopyMode="IncludeHeader" IsReadOnly="True" IsTabStop="True" AutoGeneratedColumns="dataGridView_AutoGeneratedColumns" />

后面有一些代码来填充它:

private DataTable dataTable;

public Constructor()
{
    ....
    this.dataTable = new DataTable();

    dataGridView.DataContext = dataTable;
    ....
}
于 2009-05-11T13:50:56.170 回答