public static void ExportDataGridViewToTSV(DataTable DGV_Data, string strFileName) { try { string str = ""; string caption = ""; string s = ""; FileStream stream = File.Open(strFileName, FileMode.Create, FileAccess.Write); for (int i = 0; i < DGV_Data.Rows.Count; i++) { byte[] bytes; for (int j = 0; j < DGV_Data.Columns.Count; j++) { caption = DGV_Data.Columns[j].Caption; if (caption.ToLower().IndexOf("date") >= 0) { str = ClsSystem.gnvl(DGV_Data.Rows[i].ItemArray[j], ""); if (str != "") { str = Convert.ToDateTime(str).ToString("yyyy-MM-dd"); } } else if ((caption.ToLower().IndexOf("quantity") >= 0) || (caption.IndexOf("amount") >= 0)) { str = Convert.ToDecimal(ClsSystem.gnvl(DGV_Data.Rows[i].ItemArray[j], "0")).ToString("0.000"); } else { str = ClsSystem.gnvl(DGV_Data.Rows[i].ItemArray[j], ""); } if (j == 0) { s = str; } else { s = s + "\t" + str; } } if (i == (DGV_Data.Rows.Count - 1)) { bytes = Encoding.UTF8.GetBytes(s); } else { bytes = Encoding.UTF8.GetBytes(s + "\r\n"); } stream.Write(bytes, 0, bytes.Length); } stream.Close(); } catch (Exception exception) { MessageBox.Show(exception.ToString()); } }
public static void ExportDataGridViewToExcel(DataGridView dataGridview1) { SaveFileDialog dialog = new SaveFileDialog { Filter = "Execl files (*.xls)|*.xls", FilterIndex = 0, RestoreDirectory = true, CreatePrompt = true, Title = "导出Excel文件到" }; dialog.ShowDialog(); Stream stream = dialog.OpenFile(); StreamWriter writer = new StreamWriter(stream, Encoding.GetEncoding("gb2312")); try { for (int i = 0; i < dataGridview1.Rows.Count; i++) { string str = ""; for (int j = 0; j < dataGridview1.Columns.Count; j++) { if (j > 0) { str = str + "\t"; } str = str + ClsSystem.gnvl(dataGridview1.Rows[i].Cells[j].Value, ""); } writer.WriteLine(str); } writer.Close(); stream.Close(); } catch (Exception exception) { MessageBox.Show(exception.ToString()); } finally { writer.Close(); stream.Close(); } }
private void cb_ok_Click(object sender, EventArgs e) { try { ClsXML.creatXML("StockSet.xml", "ServerSet"); ClsXML.removeAllElement("StockSet.xml"); DataSet ds = new DataSet(); ds.Tables.Add(); ds.Tables[0].Clear(); ds.Tables[0].Columns.Add("Server"); ds.Tables[0].Columns.Add("Database"); ds.Tables[0].Columns.Add("User"); ds.Tables[0].Columns.Add("Password"); ds.Tables[0].Columns.Add("ItemMaster"); ds.Tables[0].Columns.Add("Customer"); ds.Tables[0].Columns.Add("InvStock"); ds.Tables[0].Columns.Add("InvRK"); ds.Tables[0].Columns.Add("InvCK"); ds.Tables[0].Columns.Add("InvCKMonth"); ds.Tables[0].Columns.Add("HKVendor"); ds.Tables[0].Columns.Add("HKCustomer"); ds.Tables[0].Rows.Add(new object[0]); ds.Tables[0].Rows[0]["Server"] = ClsSystem.gnvl(this.textBoxServer.Text, ""); ds.Tables[0].Rows[0]["Database"] = ClsSystem.gnvl(this.textDatabase.Text, ""); ds.Tables[0].Rows[0]["User"] = ClsSystem.gnvl(this.textUser.Text, ""); ds.Tables[0].Rows[0]["Password"] = ClsSystem.gnvl(this.textPassword.Text, ""); ds.Tables[0].Rows[0]["ItemMaster"] = ClsSystem.gnvl(this.TItemMaster.Text, ""); ds.Tables[0].Rows[0]["Customer"] = ClsSystem.gnvl(this.TCustomer.Text, ""); ds.Tables[0].Rows[0]["InvStock"] = ClsSystem.gnvl(this.TInvStock.Text, ""); ds.Tables[0].Rows[0]["InvRK"] = ClsSystem.gnvl(this.TInvRK.Text, ""); ds.Tables[0].Rows[0]["InvCK"] = ClsSystem.gnvl(this.TInvCK.Text, ""); ds.Tables[0].Rows[0]["InvCKMonth"] = ClsSystem.gnvl(this.TInvCKMonth.Text, ""); ds.Tables[0].Rows[0]["HKVendor"] = ClsSystem.gnvl(this.THKVendor.Text, ""); ds.Tables[0].Rows[0]["HKCustomer"] = ClsSystem.gnvl(this.THKCustomer.Text, ""); ClsXML.addElement("StockSet.xml", ds); MessageBox.Show("保存成功!", "", MessageBoxButtons.OK); } catch (Exception exception) { MessageBox.Show(exception.Message.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand); } }