如何将数据从列表视图sqllite 数据库导出到 txt 文件?

问题描述

我有来自 sqlite 数据库的数据并将其显示在 Xamarin Forms 的列表视图中。现在我想将列表视图中的内容导出到特定位置的 txt 文件中。

我一直在互联网上搜索,但没有得到任何适当的解决方案。

这是我绑定的来源。

 <?xml version="1.0" encoding="utf-8" ?>
  <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:Xamarinsqlite"
         x:Class="Xamarinsqlite.MainPage">

<StackLayout>
    <StackLayout>
        <StackLayout HorizontalOptions="Center" VerticalOptions="Start">
            <Image x:Name="imgBanner" Source="banner.png" ></Image>
            <Image Margin="0,10" HeightRequest="100" Source="sqlite.png" ></Image>
            <Label Margin="0,10" Text="sqlite" FontAttributes="Bold" FontSize="Large" TextColor="Gray" HorizontalTextAlignment="Center" ></Label>
            <Entry x:Name="txtPersonId" Placeholder="PersonId Update and Delete"></Entry>
            <Entry x:Name="txtName" Placeholder="Enter Person Name"></Entry>
            <StackLayout  HorizontalOptions="CenterandExpand" Orientation="Horizontal">
                <Button x:Name="btnAdd" WidthRequest="200" Text="Add" Clicked="BtnAdd_Clicked" />
                <Button x:Name="btnRead" WidthRequest="200" Text="Read" Clicked="BtnRead_Clicked" />
            </StackLayout>
            <StackLayout HorizontalOptions="CenterandExpand" Orientation="Horizontal">
                <Button x:Name="btnUpdate" WidthRequest="200" Text="Update" Clicked="BtnUpdate_Clicked"/>
                <Button x:Name="btnDelete" WidthRequest="200" Text="Delete" Clicked="BtnDelete_Clicked" />
                <Button x:Name="btnExport" WidthRequest="200" Text="Export" Clicked="btnExport_Clicked" />
            </StackLayout>
            <ListView x:Name="lstPersons">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextCell Text="{Binding Name}" Detail="{Binding PersonID}"></TextCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

        </StackLayout>
    </StackLayout>
</StackLayout>

背后的代码

using System;
using Xamarin.Forms;

namespace Xamarinsqlite
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        protected async override void OnAppearing()
        {
            base.OnAppearing();
        
            //Get All Persons
            var personList = await App.sqliteDb.GetItemsAsync();
            if(personList!=null)
            {
                lstPersons.ItemsSource = personList;
            }
        }

        private async void BtnAdd_Clicked(object sender,EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtName.Text))
            {
                Person person = new Person()
                {
                    Name = txtName.Text
                };

                //Add New Person
                await App.sqliteDb.SaveItemAsync(person);
                txtName.Text = string.Empty;
                await displayAlert("Success","Person added Successfully","OK");
                //Get All Persons
                var personList = await App.sqliteDb.GetItemsAsync();
                if (personList != null)
                {
                    lstPersons.ItemsSource = personList;
                }
            }
            else
            {
                await displayAlert("required","Please Enter name!","OK");
            }
        }

        private async void BtnRead_Clicked(object sender,EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtPersonId.Text))
            {
                //Get Person
                var person = await App.sqliteDb.GetItemAsync(Convert.ToInt32(txtPersonId.Text));
                if(person!=null)
                {
                    txtName.Text = person.Name;
                    await displayAlert("Success","Person Name: "+ person.Name,"OK");
                }
            }
            else
            {
                await displayAlert("required","Please Enter PersonID","OK");
            }
        }

        private async void BtnUpdate_Clicked(object sender,EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtPersonId.Text))
            {
                Person person = new Person()
                {
                    PersonID=Convert.ToInt32(txtPersonId.Text),Name = txtName.Text
                };

                //Update Person
                await App.sqliteDb.SaveItemAsync(person);

                txtPersonId.Text = string.Empty;
                txtName.Text = string.Empty;
                await displayAlert("Success","Person Updated Successfully","OK");
                //Get All Persons
                var personList = await App.sqliteDb.GetItemsAsync();
                if (personList != null)
                {
                    lstPersons.ItemsSource = personList;
                }

            }
            else
            {
                await displayAlert("required","OK");
            }
        }

        private async void BtnDelete_Clicked(object sender,EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtPersonId.Text))
            {
                //Get Person
                var person = await App.sqliteDb.GetItemAsync(Convert.ToInt32(txtPersonId.Text));
                if (person != null)
                {
                    //Delete Person
                    await App.sqliteDb.DeleteItemAsync(person);
                    txtPersonId.Text = string.Empty;
                    await displayAlert("Success","Person Deleted","OK");
                
                    //Get All Persons
                    var personList = await App.sqliteDb.GetItemsAsync();
                    if (personList != null)
                    {
                        lstPersons.ItemsSource = personList;
                    }
                }
            }
            else
            {
                await displayAlert("required","OK");
            }
        }

        private void btnExport_Clicked(object sender,EventArgs e)
        {
       //How to Export Data In Txt file
        }
    }
}

请帮助我 我是 Xamarin 的新手

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)