using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Windows.Forms; namespace AllBase { public class OutExcel { /// /// 保存数据 /// /// /// /// public static bool SaveFile(string path, 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(path)) File.Delete(path); File.WriteAllText(path, 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(path, temp, Encoding.Unicode); temp = ""; } catch { //return false; } } } if (string.IsNullOrEmpty(temp)) return true; try { File.AppendAllText(path, 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; } public static bool SaveFile(string path, bool isNewFile, bool isShowHideColumn, List dgvRows, params string[] exceptHeaderText) { if (dgvRows == null || dgvRows.Count == 0) return false; var dt = dgvRows.FirstOrDefault()?.DataGridView; if (dt == null) return false; string temp = ""; if (isNewFile) { for (int i = 0; i < dt.ColumnCount; i++) { if ((isShowHideColumn || dt.Columns[i].Visible) && !exceptHeaderText.Contains(dt.Columns[i].HeaderText)) temp += "\"" + dt.Columns[i].HeaderText + "\"\t"; } if (!string.IsNullOrEmpty(temp)) temp += "\r\n"; try { if (File.Exists(path)) File.Delete(path); File.WriteAllText(path, temp, Encoding.Unicode); temp = ""; } catch { return false; } } for (int i = 0; i < dt.RowCount; i++) { if (!dgvRows.Contains(dt.Rows[i])) continue; for (int j = 0; j < dt.ColumnCount; j++) { if ((isShowHideColumn || dt.Columns[j].Visible) && !exceptHeaderText.Contains(dt.Columns[j].HeaderText)) temp += "\"" + dt.Rows[i].Cells[j].Value + "\"\t"; } temp += "\r\n"; if (temp.Length > 2000) { try { File.AppendAllText(path, temp, Encoding.Unicode); temp = ""; } catch { try { if (File.Exists(path)) File.Delete(path); } catch { } return false; } } } if (string.IsNullOrEmpty(temp)) return true; try { File.AppendAllText(path, temp, Encoding.Unicode); } catch { try { if (File.Exists(path)) File.Delete(path); } catch { } return false; } return true; } } }