解决数据为空,隐藏列不导出! public static void ExportExcel(DataGridView dgvList) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "文本文件 (*.txt)|*.txt|Excel 文件(*.xls)|*.xls"; saveFileDialog.FilterIndex = 0; saveFileDialog.RestoreDirectory = true; saveFileDialog.CreatePrompt = false; saveFileDialog.Title = "保存日志"; saveFileDialog.CheckFileExists = false; if (saveFileDialog.ShowDialog() != DialogResult.OK) return; Stream myStream; myStream = saveFileDialog.OpenFile(); //StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312")); StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0)); string str = ""; try { //写标题 for (int i = 0; i < dgvList.ColumnCount; i++) { if (i > 0) { if (!dgvList.Columns[i].Visible) { str += ""; } else { str += "/t"; } } if (dgvList.Columns[i].Visible) { str += dgvList.Columns[i].HeaderText; } } sw.WriteLine(str); //写内容 for (int j = 0; j < dgvList.Rows.Count; j++) { string tempStr = ""; for (int k = 0; k < dgvList.Columns.Count; k++) { if (k > 0) { if (!dgvList.Rows[j].Cells[k].Visible) { tempStr += ""; } else { tempStr += "/t"; } } if (dgvList.Rows[j].Cells[k].Visible) { tempStr += (dgvList.Rows[j].Cells[k].Value+"").Replace("/r", "").Replace("/t", "").Replace("/n", ""); } } sw.WriteLine(tempStr); } sw.Close(); myStream.Close(); } catch (Exception exc) { MessageBox.Show(exc.ToString()); } finally { sw.Close(); myStream.Close(); } } 调用 Class.ExportExcel(dgv);