/// <summary> /// 保存文件 /// 塗聚文 /// 2014-08-29 /// Geovin Du /// </summary> /// <param name="dGV"></param> /// <param name="filename"></param> public static void DataGridViewToCsV(DataGridView dGV, string filename) { string stOutput = ""; // Export titles: string sHeaders = ""; for (int j = 0; j < dGV.Columns.Count; j++) sHeaders = sHeaders.ToString() + Convert.ToString(dGV.Columns[j].HeaderText) + ", "; stOutput += sHeaders + "\r\n"; // Export data. for (int i = 0; i < dGV.RowCount - 1; i++) { string stLine = ""; for (int j = 0; j < dGV.Rows[i].Cells.Count; j++) stLine = stLine.ToString() + Convert.ToString(dGV.Rows[i].Cells[j].Value) + ", "; stOutput += stLine + "\r\n"; } UTF8Encoding utf8 = new UTF8Encoding(); string file = "1.csv"; SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);//Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); saveFileDialog1.CreatePrompt = true; saveFileDialog1.OverwritePrompt = true; saveFileDialog1.Title = "Save text Files"; //saveFileDialog1.CheckFileExists = true; //saveFileDialog1.CheckPathExists = true; saveFileDialog1.DefaultExt = "csv"; saveFileDialog1.Filter = "csv files (*.csv)|*.csv|All files (*.*)|*.*"; saveFileDialog1.FilterIndex = 1; saveFileDialog1.RestoreDirectory = true; System.IO.Stream fileStream; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { //FileStream fs = new FileStream(filename, FileMode.Create); file = saveFileDialog1.FileName; fileStream = saveFileDialog1.OpenFile(); MemoryStream userInput = new MemoryStream(); byte[] output = Encoding.Default.GetBytes(stOutput); //fileStream.Read(output, 0, (int)output.Length); fileStream.Write(output, 0, output.Length); fileStream.Close(); } } /// <summary> /// 保存文件 /// 塗聚文 /// 2014-08-28 /// Geovin Du /// </summary> /// <param name="dGV"></param> /// <param name="filename"></param> public static void DataGridViewToCsVDu(DataGridView dGV, string filename) { string stOutput = ""; // Export titles: string sHeaders = ""; for (int j = 0; j < dGV.Columns.Count; j++) sHeaders = sHeaders.ToString() + Convert.ToString(dGV.Columns[j].HeaderText) + ", "; stOutput += sHeaders + "\r\n"; // Export data. for (int i = 0; i < dGV.RowCount - 1; i++) { string stLine = ""; for (int j = 0; j < dGV.Rows[i].Cells.Count; j++) stLine = stLine.ToString() + Convert.ToString(dGV.Rows[i].Cells[j].Value) + ", "; stOutput += stLine + "\r\n"; } UTF8Encoding utf8 = new UTF8Encoding(); //Encoding utf16 = Encoding.GetEncoding(1200);//unicode encoding byte[] output = Encoding.Default.GetBytes(stOutput); FileStream fs = new FileStream(filename, FileMode.Create); BinaryWriter bw = new BinaryWriter(fs, Encoding.Default); bw.Write(output, 0, output.Length); //write the encoded file bw.Flush(); bw.Close(); fs.Close(); }
csv格式的數據其實直接用IO的openText就行,讀出都是字符型,關鍵是換行符你要區別好。
//我從網上下載了這段代碼
//這段代碼的主要意思是 判斷導出文件是否已經存在,如果文件已經存在就刪除掉
// 打開文件 StreamWriter sw = new StreamWriter(new FileStream(strPath, ileMode.CreateNew), Encoding.GetEncoding("GB2312"));
把字段名寫入了文件
//二層循環寫入數據
//注意字段的間隔符是 ","
//關閉文件 這樣就可以
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Data;
namespace Utility
{
public class CSVHelper
{
//導出為svc文件,strFileName為要導出的csv格式文件的路徑和文件名:比如,"d: est est.csv"
public void ExportToSvc(System.Data.DataTable dt, string strFileName)
{
string strPath = strFileName;
if (File.Exists(strPath))
{
File.Delete(strPath);
} //如果文件已經存在就刪除掉
//先打印標頭
StringBuilder strColu = new StringBuilder();
StringBuilder strValue = new StringBuilder();
int i = 0;
try
{
StreamWriter sw = new StreamWriter(new FileStream(strPath, FileMode.CreateNew), Encoding.GetEncoding("GB2312"));
for (i = 0; i <= dt.Columns.Count - 1; i++)
{
strColu.Append(dt.Columns.ColumnName);
strColu.Append(","); //csv的分隔符是","
}
strColu.Remove(strColu.Length - 1, 1);//移出掉最後一個,字符
......余下全文>>