using System.IO; using System.Text; using System.Windows.Forms; namespace AllBase { public class OutExcel { /// /// 保存数据 /// /// /// /// public static bool SaveFile(string patch, DataGridView dt) { string temp = ""; for (int i = 0; i < dt.ColumnCount; i++) { if (dt.Columns[i].Visible && dt.Columns[i].HeaderText != "删除" && dt.Columns[i].HeaderText != "历史记录") temp += dt.Columns[i].HeaderText + "\t"; } if (!string.IsNullOrEmpty(temp)) temp += "\r\n"; try { if (File.Exists(patch)) File.Delete(patch); File.WriteAllText(patch, temp, Encoding.Unicode); temp = ""; } catch { return false; } for (int i = 0; i < dt.RowCount; i++) { for (int j = 0; j < dt.ColumnCount; j++) { if (dt.Columns[j].Visible && dt.Columns[j].HeaderText != "删除" && dt.Columns[j].HeaderText != "历史记录") temp += dt.Rows[i].Cells[j].Value + "\t"; } temp += "\r\n"; if (temp.Length > 2000) { try { File.AppendAllText(patch, temp, Encoding.Unicode); temp = ""; } catch { //return false; } } } if (string.IsNullOrEmpty(temp)) return true; try { File.AppendAllText(patch, temp, Encoding.Unicode); } catch { return false; } return true; } public static bool SaveFileCheckBox(string patch, DataGridView dt) { string temp = ""; for (int i = 0; i < dt.ColumnCount; i++) { if (dt.Columns[i].HeaderText != "删除" && dt.Columns[i].HeaderText != "全选") temp += dt.Columns[i].HeaderText + "\t"; } if (!string.IsNullOrEmpty(temp)) temp += "\r\n"; try { if (File.Exists(patch)) File.Delete(patch); File.WriteAllText(patch, temp, Encoding.Unicode); temp = ""; } catch { return false; } for (int i = 0; i < dt.RowCount; i++) { if (bool.Parse(dt.Rows[i].Cells["isCheckBoxColumny"].Value.ToString()) == false) { continue; } for (int j = 0; j < dt.ColumnCount; j++) { if (dt.Columns[j].HeaderText != "删除" && dt.Columns[j].HeaderText != "全选") { temp += dt.Rows[i].Cells[j].Value + "\t"; } } temp += "\r\n"; if (temp.Length > 2000) { try { File.AppendAllText(patch, temp, Encoding.Unicode); temp = ""; } catch { //return false; } } } if (string.IsNullOrEmpty(temp)) return true; try { File.AppendAllText(patch, temp, Encoding.Unicode); } catch { return false; } return true; } } }