我有来自mysql数据库的数据,并在listview中显示了它。现在,我想将列表视图中的内容导出为pdf文件或可读文件。
我一直在搜索,它指示我趋向于融合,但是没有足够的教程。也许我需要添加一些细节才能完成此事件。
这是我绑定的来源。
public class Post
{
//private const string V = "test";
//from settingspage to show up in history
string rain1lbl = Settings.rain1LocationSettings;
string rain2lbl = Settings.rain2LocationSettings;
string rain3lbl = Settings.rain3LocationSettings;
//ID primary key that we will autoincrement
//These are binding source for Historypage
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
public static bool showLabel { get; set; } //public class model
public string rain1Lbl
{
get => rain1lbl;
set
{
rain1lbl = Settings.rain1LocationSettings;
}
}
public string rain2Lbl
{
get => rain2lbl;
set
{
rain2lbl = Settings.rain2LocationSettings;
}
}
public string rain3Lbl
{
get => rain3lbl;
set
{
rain3lbl = Settings.rain3LocationSettings;
}
}
public string CDateTime { get; set; }
[MaxLength(3)]
public string rain1vol { get; set; }
[MaxLength(3)]
public string rain2vol { get; set; }
[MaxLength(3)]
public string rain3vol { get; set; }
}
}
xaml文件
<ListView x:Name="postListView" SeparatorVisibility="Default" HasUnevenRows="true"
ItemsSource="{Binding Items}" SeparatorColor="White" ItemSelected="listviewhandle_ItemSelected" >
<ListView.ItemTemplate>
<!-- from the post.cs -->
<DataTemplate>
<ViewCell >
<Grid BackgroundColor="Black" HorizontalOptions="CenterAndExpand" VerticalOptions="FillAndExpand"
Padding="1,2,1,0">
<Grid HorizontalOptions="CenterAndExpand" VerticalOptions="FillAndExpand" ColumnSpacing="1"
RowSpacing="1">
<Grid.RowDefinitions >
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<!-- Date Time binding from Post.cs MVVM-->
<Label x:Name="datetimeLabel" Grid.Row="0" HorizontalTextAlignment="Center" Text="{Binding CDateTime}" BackgroundColor="Yellow" Grid.ColumnSpan="2" />
<!-- rain1 Row 1 odd rows are conrflowerblue-->
<Label Grid.Row="1" FontSize="Medium" Grid.Column="0" Text="{Binding rain1Lbl}" HorizontalTextAlignment="Center" BackgroundColor="cornflowerblue" />
<Label Grid.Row="1" Grid.Column="1" Text="{Binding rain1vol}" HorizontalTextAlignment="Center" BackgroundColor="cornflowerblue" />
<!-- endrow1 -->
<!-- rain2 Row 2 -->
<Label Grid.Row="2" FontSize="Medium" Grid.Column="0" IsVisible="{Binding showLabel}" Text="{Binding rain2Lbl}" HorizontalTextAlignment="Center" BackgroundColor="Yellow"/>
<Label Grid.Row="2" Grid.Column="1" Text="{Binding rain2vol}" HorizontalTextAlignment="Center" BackgroundColor="Yellow" />
<!-- endrow2 -->
<!-- rain3 Row 3 -->
<Label Grid.Row="3" FontSize="Medium" Grid.Column="0" Text="{Binding rain3Lbl}" HorizontalTextAlignment="Center" BackgroundColor="cornflowerblue" />
<Label Grid.Row="3" Grid.Column="1" Text="{Binding rain3vol}" HorizontalTextAlignment="Center" BackgroundColor="cornflowerblue"/>
<!-- endrow3 -->
</Grid>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
cs文件 公共局部类HistoryLogPage:ContentPage { 公共HistoryLogPage() {
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
SQLiteConnection conn = new SQLiteConnection(App.DatabaseLocation);
{
conn.CreateTable<Post>();
var posts= conn.Table<Post>().ToList();
postListView.ItemsSource = posts;
conn.Close();
}
}
//to be able to know which item was selected
void listviewhandle_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var selectedPost = postListView.SelectedItem as Post;
if(selectedPost != null)
{
Navigation.PushModalAsync(new PostDetail(selectedPost));
}
}
void converbuttonHandle_Clicked(object sender, EventArgs e)
{
}
}
答案 0 :(得分:0)
根据您的描述,我找到了一种将数据从ListView导出到Excel文件的方法,您可以看一下:
https://github.com/egbakou/ExportDataToExcel
更新:
// Constructing header
Row row = new Row();
row.Append(
ConstructCell("No", CellValues.String),
ConstructCell("FullName", CellValues.String),
ConstructCell("Phone", CellValues.String)
);
// Insert the header row to the Sheet Data
sheetData.AppendChild(row);
// Add each product
foreach (var d in Developers)
{
row = new Row();
row.Append(
ConstructCell(d.ID.ToString(), CellValues.String),
ConstructCell(d.FullName, CellValues.String),
ConstructCell(d.Phone, CellValues.String));
sheetData.AppendChild(row);
}
根据代码,您可以使用Row.Append()在excel中打印标题,例如列1,列2,列3 ......
然后,您可以让每个ListView项源在Excel中插入数据。通过您的代码,我看到您的ListView Itemsource是Items,因此您可以让每个Items在excel中插入数据,如下所示:
foreach (var d in Items)
{
row = new Row();
row.Append(
ConstructCell(d.CDateTime.ToString(), CellValues.String),
ConstructCell(d.rain1Lbl.ToString(), CellValues.String),
ConstructCell(d.rain1vol.ToString(), CellValues.String)
ConstructCell(d.showLabel.ToString(), CellValues.String),
ConstructCell(d.rain2vol.ToString(), CellValues.String),
ConstructCell(d.rain3Lbl.ToString(), CellValues.String),
ConstructCell(d.rain3vol.ToString(), CellValues.String));
sheetData.AppendChild(row);
}
请