today ,i will improve my software's memory way,sqlite to excel,because using sql will employ system recourse.
so first problem i meet is how to get the path from a explore.I used following code
OpenFileDialog fd = new OpenFileDialog();
fd.InitialDirectory = _path;
fd.Filter = "xls files (*.xls)|*.xls|All files (*.*)|*.*";
if (fd.ShowDialog(this) == DialogResult.OK)
{
MessageBox.Show(fd.FileName);
}
this openfiledialog used regular expression to filte files,just show files that end with ".xls",and i will get the file'path if user click "ok".
if you just want to open the explore ,you can do it by following code ,more simply.
System.Diagnostics.Process.Start(_path);
so when i got the path i will use the stream to parse file content.because the ".csv" file is more efficient when the data is large,i create a temporary file in CSV format to copy the file user selected.
_helper = new ExcelHelper(fd.FileName, fd.FileName.Replace(".xls", ".csv"));
_helper.SaveAsFile(SaveAsFileFormat.CSV);
FileStream s = File.OpenRead(fd.FileName.Replace(".xls",".csv"));
CSVReader csv = new CSVReader(s);
i display the data with datagridview,this way need a datatable to bind the view.so i find some code to do it .
/// <summary>
/// 把一个M行N列的二维数组转换为DataTable
/// </summary>
/// <param name="ColumnNames">一维数组,代表列名,不能有重复值</param>
/// <param name="Arrays">M行N列的二维数组</param>
/// <returns>返回DataTable</returns>
public static DataTable Convert(string[] ColumnNames, string[,] Arrays)
{
DataTable dt = new DataTable();
foreach (string ColumnName in ColumnNames)
{
dt.Columns.Add(ColumnName, typeof(string));
}
for (int i1 = 0; i1 < Arrays.GetLength(0); i1++)
{
DataRow dr = dt.NewRow();
for (int i = 0; i < ColumnNames.Length; i++)
{
dr[i] = Arrays[i1, i].ToString();
}
dt.Rows.Add(dr);
}
return dt;
}
but there are some problems, the stream of c# can't reset,when i readline until to end,i can read again,and however i do,the stream is the only.can't read twice.
so it's a serious problem ,that mean i can't count and read data at the same time.if i can't count i would't to declare a array.
and then i use arraylist ,but it have problem too . how to write a cycle,how i judge whether the data had finished reading.
now i had solved the last problem ,
FileStream s = File.OpenRead(fd.FileName.Replace(".xls",".csv"));
CSVReader csv = new CSVReader(s);
ArrayList tmpArray = new ArrayList();
Boolean isend=false;
while (!isend){
string[] temp_str = csv.GetCSVLine();
if (temp_str != null)
{
tmpArray.Add(temp_str);
MessageBox.Show(temp_str[1]);
}
else
{
isend = true;
}
}
string[,] data=new string[tmpArray.Count,4];
for(int i=0;i<data.GetLength(0);i++){
string []temp=(string[])tmpArray[i];
for (int j = 0; j < 4; j++) {
data[i, j] = temp[j];
}
}
dataGridView1.DataSource = ArrayToDataTable.Convert(new string[]{"学号","姓名","班级","卡号"},data);
there is a new problem ,which is Chinese garbled .
haha ,i almost forgot the jsp teacher had tought me.
so easy by use this code
CSVReader csv = new CSVReader(s, Encoding.GetEncoding("gb2312"));