导出到 CSV 在使用 c# 的移动手持设备中不起作用

Export to CSV not working in mobile handheld device using c#

我正在使用 Mobile Handheld 设备应用程序,使用 c#,试图将数据表导出到 csv 文件,但是 none方法对我有用, 平台 Visual Studio 2008 智能设备应用程序 File.WriteALLText 方法在智能设备应用程序中不起作用。

   private void btnsubmit_Click(object sender, EventArgs e)
    {
        TextWriter txt = new StreamWriter("logs.txt");

        try
        {
            if (!string.IsNullOrEmpty(txtOutput.Text) && !string.IsNullOrEmpty(txtqty.Text))
            {

                DataTable dt;
                dt = new DataTable();
                dt.Columns.Add("Barcode", System.Type.GetType("System.String"));
                dt.Columns.Add("Location", System.Type.GetType("System.String"));
                dt.Columns.Add("Quantity", System.Type.GetType("System.String"));
                DataRow dr = dt.NewRow();
                dr["Barcode"] = txtOutput.Text.Trim();
                dr["Location"] = "123";
                dr["Quantity"] = txtqty.Text.Trim();
                dt.Rows.Add(dr);                    
                StringBuilder data = ConvertDataTableToCsvFile(dt);
                SaveData(data, @"Data.csv");

                MessageBox.Show("Data exported Success");
            }
            else
            {
                MessageBox.Show("Please enter all value");
            }

            txt.Close();
        }
        catch (Exception ex)
        {
            string errmsg = ex.Message.ToString();

            MessageBox.Show("error " + errmsg);
        }
        txt.Close();
    }

    public StringBuilder ConvertDataTableToCsvFile(DataTable dtData)
    {
        StringBuilder data = new StringBuilder();


        for (int column = 0; column < dtData.Columns.Count; column++)
        {
            //Making sure that end of the line, shoould not have comma delimiter.
            if (column == dtData.Columns.Count - 1)
                data.Append(dtData.Columns[column].ColumnName.ToString().Replace(",", ";"));
            else
                data.Append(dtData.Columns[column].ColumnName.ToString().Replace(",", ";") + ',');
        }

        data.Append(Environment.NewLine);//New line after appending columns.

        for (int row = 0; row < dtData.Rows.Count; row++)
        {
            for (int column = 0; column < dtData.Columns.Count; column++)
            {

                if (column == dtData.Columns.Count - 1)
                    data.Append(dtData.Rows[row][column].ToString().Replace(",", ";"));
                else
                    data.Append(dtData.Rows[row][column].ToString().Replace(",", ";") + ',');
            }


            if (row != dtData.Rows.Count - 1)
                data.Append(Environment.NewLine);
        }
        return data;
    }
    public void SaveData(StringBuilder data, string filePath)
    {
        using (StreamWriter objWriter = new StreamWriter(filePath))
        {
            objWriter.WriteLine(data);
        }
    }

几年前,当我开始使用带有 Windows 移动设备的手持设备时,我遇到过同样的问题。您应该在 SaveData 中添加 objWriter.Flush()

public void SaveData(StringBuilder data, string filePath)
    {
        using (StreamWriter objWriter = new StreamWriter(filePath))
        {
            objWriter.WriteLine(data);
            objWriter.Flush();
        }
    }