求c#将dataGridView1中的数据导出到Excel文件中,Excel导出
求c#将dataGridView1中的数据导出到Excel文件中,Excel导出。有没有NPOI的导出方法?
链接: https://pan.baidu.com/s/1gAOhN7hPHoZy__4H939Cuw 提取码: h72c 复制这段内容后打开百度网盘手机App,操作更方便哦
求代下代下CSDN:
https://download.csdn.net/download/weixin_38659622/12747021
https://www.52pojie.cn/thread-1421980-1-1.html
本帖最后由 Jack2002 于 2021-4-22 08:53 编辑
https://www.jb51.net/article/169260.htm
https://www.cnblogs.com/hailexuexi/p/5323972.html
这不是现在的?CSDN上分享东西99%都很简单,因为VS的版本不同,下载下来你不一定能用,
另一个是CSDN现在下载一个文件要50分,我攒了10多年的分也就够下载几个文件!因为之前分享的资源都是1分2分几分的,所以没攒到多少分! 本帖最后由 雪色的夏天 于 2021-4-22 09:13 编辑
[*]private void ExportToExcel()
[*]{
[*] SaveFileDialog saveFileDialog = new SaveFileDialog();
[*] saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
[*] saveFileDialog.FilterIndex = 0;
[*] saveFileDialog.RestoreDirectory = true;
[*] saveFileDialog.CreatePrompt = true;
[*] saveFileDialog.Title = "导出到Excel";
[*] saveFileDialog.ShowDialog();
[*] if (saveFileDialog.FileName == "")
[*] {
[*] return;
[*] }
[*] Stream myStream = saveFileDialog.OpenFile();
[*] StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
[*] string str = "";
[*] try
[*] {
[*] for (int i = 0; i < dataGridView1.ColumnCount; i++)
[*] {
[*] if (dataGridView1.Columns[i].Visible == false || dataGridView1.Columns[i].DataPropertyName == "")
[*] {
[*] continue;
[*] }
[*] str += dataGridView1.Columns[i].HeaderText;
[*] str += "\t";
[*] }
[*] sw.WriteLine(str);
[*]
[*] for (int j = 0; j < dataGridView1.Rows.Count - 1; j++)
[*] {
[*] string strTemp = "";
[*] for (int k = 0; k < dataGridView1.Columns.Count; k++)
[*] {
[*] if (dataGridView1.Columns[k].Visible == false || dataGridView1.Columns[k].DataPropertyName == "")
[*] {
[*] continue;
[*] }
[*] object obj = dataGridView1.Rows[j].Cells[k].Value;
[*]
[*] if (obj != null)
[*] {
[*] strTemp += dataGridView1.Rows[j].Cells[k].Value.ToString();
[*] }
[*] else
[*] {
[*] strTemp = "";
[*] }
[*] strTemp += "\t";
[*] }
[*] sw.WriteLine(strTemp);
[*] }
[*]
[*] sw.Close();
[*] myStream.Close();
[*]
[*] MessageBox.Show("成功导出到Excel文件:\n" + saveFileDialog.FileName);
[*] }
[*] catch (Exception ex)
[*] {
[*] MessageBox.Show(ex.ToString());
[*] }
[*] finally
[*] {
[*] sw.Close();
[*] myStream.Close();
[*] }
[*]}
添加引用:Microsoft.Office.Interop.Excel
if (dataGridView1.Rows.Count>0)
{
Microsoft.Office.Interop.Excel.Application xcelApp = new Microsoft.Office.Interop.Excel.Application();
xcelApp.Application.Workbooks.Add(Type.Missing);
//打印表头
for (int i = 1;i<dataGridView1.Columns.Count+1;i++)
{
xcelApp.Cells = dataGridView1.Columns.HeaderText;
}
//打印内容
for (int i = 0; i < dataGridView1.Rows.Count; i++)//行
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)//列
{
xcelApp.Cells = dataGridView1.Rows.Cells.Value.ToString();
}
}
xcelApp.Columns.AutoFit();
xcelApp.Visible = true;
} 我用的是这个,有点慢,不过挺好使
private void ExportToExcel()
{
// Creating a Excel object.
Microsoft.Office.Interop.Excel._Application excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = excel.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
try
{
worksheet = workbook.ActiveSheet;
worksheet.Name = "ExportedFromDatGrid";
int cellRowIndex = 1;
int cellColumnIndex = 1;
//Loop through each row and read value from each column.
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
// Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
if (cellRowIndex == 1)
{
worksheet.Cells = dataGridView1.Columns.HeaderText;
}
else
{
worksheet.Cells = dataGridView1.Rows.Cells.Value.ToString();
}
cellColumnIndex++;
}
cellColumnIndex = 1;
cellRowIndex++;
}
//Getting the location and file name of the excel to save from user.
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
saveDialog.FilterIndex = 2;
if (saveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
workbook.SaveAs(saveDialog.FileName);
MessageBox.Show("导出成功");
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
excel.Quit();
workbook = null;
excel = null;
}
} 简单一点的直接写csv文件,每行字段之间用逗号分隔。
或者用NPOI写excel,看下官方api不复杂的 Jack2002 发表于 2021-4-22 08:45
https://www.jb51.net/article/169260.htm
https://www.cnblogs.com/hailexuexi/p/5323972.html
这不是现 ...
{:1_919:}非常感谢 雪色的夏天 发表于 2021-4-22 09:04
[*]private void ExportToExcel()
[*]{
[*] SaveFileDialog saveFileDialog = new SaveFileDialog() ...
非常感谢{:1_893:} 风中的疯子 发表于 2021-4-22 09:11
简单一点的直接写csv文件,每行字段之间用逗号分隔。
或者用NPOI写excel,看下官方api不复杂的
csv写excel怎么写?能有源码分享一下吗?
页:
[1]
2