C#操作Excel表的操作实例
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
string str_BUKRS = UserInfo.COMPANY;
string str_WERKS = this.cmb_WERKS.ComboBox.SelectedValue == null ? "" : this.cmb_WERKS.ComboBox.SelectedValue.ToString();
DataTable dt = service.get_작업관리_재고실사조정CSVBy조회(str_BUKRS, str_WERKS,UserInfo.DBINFO).Tables[0];
saveFileDialog1.FileName = Program.multlanguage.GetLanguage("재고실사")+"_" + DateTime.Now.ToString("yyyyMMddhhmmss");
saveFileDialog1.DefaultExt = "csv";
saveFileDialog1.Filter = "Excel files (*.csv)|*.csv";
saveFileDialog1.InitialDirectory = "c:\\";
DialogResult result = saveFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
//OleDbConnection ExcelConn = null;
try
{
string sFileName = @saveFileDialog1.FileName;
string str_col = "";
StreamWriter sw = new StreamWriter(sFileName, false, System.Text.Encoding.Default);
foreach (DataColumn dc in dt.Columns)
{
str_col += dc.Caption.Replace(",", "") + ",";
}
str_col += "\r\n";
sw.Write(str_col);
str_col = "";
foreach (DataRow dr in dt.Rows)
{
foreach (DataColumn dc in dt.Columns)
{
str_col += dr[dc].ToString().Replace(",", "") + ",";
}
str_col += "\r\n";
sw.Write(str_col);
str_col = "";
}
sw.Close();
}
catch { }
}
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;