问题描述
我遇到了一个问题,我无法帮助自己。
我的程序加载一个CSV(excel)文件,其中包含所有分配的数据库内置a] line1。
ex; A-1 = testcol,testcol,testcol
ex; A-2 = value1,value2,value3
现在我的东西在他们自己的专栏中读得很好,但我似乎无法正确保存它们。
一旦保存我的程序就决定添加另一列和整个空行。
所以我的文件现在出现;
ex; A-1 = testcol,testcol,testcol,column1
ex; A-2 = NULL,NULL,NULL,NULL
i想要删除Column1(并保存文件而不执行此操作)。
以及删除空行..
这是我的保存程序的样子:
I got an issue i cannot help myself out with.
My program loads an CSV (excel) file which has a database build-in all assigned in [a] line1.
ex; A-1 = testcol,testcol,testcol
ex; A-2 = value1,value2,value3
now i've got my things to read just fine in their own columns, but i cant seem to save them correctly.
once its saved my program decides to add another Column and a whole Empty Row.
so my file now occurs as;
ex; A-1 = testcol,testcol,testcol,column1
ex; A-2 = NULL,NULL,NULL,NULL
i would like to remove the Column1 (and save the file without doing this).
aswell as removing the empty row..
This is how my save procedure looks like:
/* Used to count the line(s)*/
toolStripProgressBar1.Value = 0;
// Don't save if no data is returned
if (dataGridView1.Rows.Count == 0)
{
return;
}
StringBuilder sb = new StringBuilder();
// Column headers
string columnsHeader = "";
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
columnsHeader += dataGridView1.Columns[i].Name + ",";
}
sb.Append(columnsHeader + Environment.NewLine);
// Go through each cell in the datagridview
foreach (DataGridViewRow dgvRow in dataGridView1.Rows)
{
// Make sure it's not an empty row.
if (!dgvRow.IsNewRow)
{
for (int c = 0; c < dgvRow.Cells.Count; c++)
{
// Append the cells data followed by a comma to delimit.
sb.Append(dgvRow.Cells[c].Value + ",");
}
// Add a new line in the text file.
sb.Append(Environment.NewLine);
}
}
// Load up the save file dialog with the default option as saving as a .csv file.
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "CSV files (*.csv)|*.csv";
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
int maxnumber = 100;
toolStripProgressBar1.Value = 0;
toolStripProgressBar1.Maximum = maxnumber;
for (int x = 0; x <= maxnumber - 1; x++)
{
toolStripProgressBar1.Value += 1;
// If they've selected a save location...
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(sfd.FileName, false))
{
string result;
result = Path.GetFileNameWithoutExtension(sfd.FileName);
tabPage1.Text = result;
toolStripStatusLabel1.Text = "[1]File Saved! - " + result;
// Write the stringbuilder text to the the file.
sw.WriteLine(sb.ToString());
}
}
}
i在这个网站上没有空间可以放我的负载这里有一个例子,但它低于这一行,你想要全部阅读。
i have no space here on this website to either put my load example here, but its below this line incase you do want to read it all.
OpenFileDialog openFile = new OpenFileDialog();
openFile.InitialDirectory = "c:\\"; //" // [Edit] Colour fix
openFile.Filter = " All Files (*.*) | *.*| CSV Files (*.csv) | *.csv";
openFile.FilterIndex = 2;
openFile.RestoreDirectory = true;
try
{
/* Always Clear the datagrid first before loading again.*/
if (this.dataGridView1.DataSource != null)
{
this.dataGridView1.DataSource = null;
}
else
{
this.dataGridView1.Rows.Clear();
}
/* Used to count the line(s)*/
toolStripProgressBar1.Value = 0;
if (openFile.ShowDialog() == DialogResult.OK)
{
/* Make sure combobox is cleared out before we load another one.*/
comboBox1.Items.Clear();
/* Define file name = file selected*/
string file = openFile.FileName;
StreamReader sr = new StreamReader(file);
/*Result = filename*/
string result;
result = Path.GetFileNameWithoutExtension(file);
tabPage1.Text = result;
/* gets all the lines of the csv */
string[] str = File.ReadAllLines(file);
/* creates a data table*/
DataTable dt = new DataTable();
/* gets the column headers from the first line*/
string[] temp = str[0].Split(',');
/* creates columns of the gridview base on what is stored in the temp array */
foreach (string t in temp)
{
dt.Columns.Add(t, typeof(string));
comboBox1.Items.Add(t);
}
/* gets the amount of row(s) loaded. */
var lines = File.ReadAllLines(file);
var count = lines.Length;
/* retrieves the rows after the first row and adds it to the datatable */
for (int i = 1; i < str.Length; i++)
{
toolStripProgressBar1.Value += 1;
toolStripProgressBar1.Maximum = count;
List<string> list = new List<string>(str[i].Split(','));
if (str[i].Contains('"'))
{
for (int j = 0; j < list.Count; j++)
{
while ((list[j].Split('"').Length % 2) == 1)
{
list[j] += "," + list[j + 1];
list.RemoveAt(j + 1);
}
if (list[j].StartsWith("\"") && list[j].EndsWith("\""))
list[j] = list[j].Substring(1, list[j].Length - 2);
}
}
else
{
string[] t = str[i].Split(',');
dt.Rows.Add(t);
}
}
//The datagridview will now be populated with data from all the other rows except the first.
dataGridView1.DataSource = dt;
toolStripStatusLabel1.Text = "[1]Lines loaded: " + count;
}
}
catch (Exception ex)
{
////If there is a issue, report it to the user, but also to the event logger.
MessageBox.Show("[1]Error: The CSV you selected could not be loaded \n\n" + ex.Message, "Load Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
请帮帮我,我不知道如何解决这个问题非常重要我的数据库保存正确:)
please help me, i have no idea how to fix this and its quite important i get my database saving right :)
推荐答案
// Column headers
string columnsHeader = "";
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
if (i != dataGridView1.Columns.Count - 1)
{
columnsHeader += dataGridView1.Columns[i].Name + ",";
}
else
{
columnsHeader += dataGridView1.Columns[i].Name;
}
}
sb.Append(columnsHeader + Environment.NewLine);
// Go through each cell in the datagridview
for (int q = 0; q < dataGridView1.RowCount; q++)
{
// Make sure it's not an empty row.
for (int c = 0; c < dataGridView1.Rows[q].Cells.Count; c++)
{
if (c != dataGridView1.Rows[q].Cells.Count - 1)
{
// Append the cells data followed by a comma to delimit.
sb.Append(dataGridView1.Rows[q].Cells[c].Value + ",");
}
else
{
sb.Append(dataGridView1.Rows[q].Cells[c].Value);
}
}
// Add a new line in the text file.
if (q != dataGridView1.RowCount - 1) sb.Append(Environment.NewLine);
}
非常感谢你们:))
thank you all so much :)
这篇关于删除列和空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!