gpt4 book ai didi

c# - 如何将 datagridview 单元格格式值导出到 excel?

转载 作者:行者123 更新时间:2023-12-04 20:11:40 27 4
gpt4 key购买 nike

我正在制作 Windows 窗体应用程序。

单击导出按钮时,datagridview 的数据导出到 Excel 文件。

我已经构建了该代码,并且运行良好。但是今天我更新了我的代码。

我添加 Datagridview 的 CellFormatting 事件,更改文件大小值,并显示

它到Datagridview。

然后,我导出到 excel 但在 excel 文件中,仍然显示原始数据

也就是说,原始数据是451936256,转换后的数据是431MB

在 excel 表中,它显示 451936256。

我的代码在下面

   //Button click Event
private void mtbtnExportToExcel_Click(object sender, EventArgs e)
{
DataGridView[] dgv = new DataGridView[] { mgrdWebApplication, mgrdContentDatabase, mgrdSiteCollections, mgrdSites, mgrdLists, mgridDocumentLibrary };
mtProgressStatus.Spinning = true;
ExportDataToExcel(dgv, "MigStatus");
mtProgressStatus.Spinning = false;

}
//Export gridview data to excel
private bool ExportDataToExcel(DataGridView[] dgv, string fileName)
{
string saveFileName = "";
SaveFileDialog saveDialog1 = new SaveFileDialog();
saveDialog1.DefaultExt = "xlsx";
saveDialog1.Filter = "Excel file|*.xlsx";
saveDialog1.FileName = fileName;
saveDialog1.ShowDialog();
saveFileName = saveDialog1.FileName;

if (saveFileName.IndexOf(":") < 0)
return false;

Excel.Application xlApp = new Excel.Application();

if (xlApp == null)
{
MessageBox.Show("Can`t create Excel");
return false;
}

Excel.Workbooks workBooks = xlApp.Workbooks;
Excel.Workbook workBook = xlApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Worksheets[1];

try
{
for (int index = 0; index < dgv.Length; index++)
{
for (int i = 0; i < dgv[index].ColumnCount; i++)
{
if (dgv[index].Columns[i].Visible)
workSheet.Cells[1, i + 1] = dgv[index].Columns[i].HeaderText;
}

for (int r = 0; r < dgv[index].Rows.Count; r++)
{
for (int i = 0; i < dgv[index].ColumnCount; i++)
{
if (dgv[index].Columns[i].Visible)
workSheet.Cells[r + 2, i + 1] = dgv[index].Rows[r].Cells[i].Value.ToString();
}
Application.DoEvents();
}
((Excel.Range)workSheet.Rows[1, Type.Missing]).Font.Bold = true;
workSheet.Columns.EntireColumn.AutoFit();

if (index < dgv.Length - 1)
{
workSheet = (Excel.Worksheet)workBook.Worksheets.Add();
}
}
}
catch(Exception ex)
{
//LogWrite logWrites = new LogWrite();
writeLog.LogsWrite(ex.ToString());
}

if (saveFileName != "")
{
try
{
workBook.Saved = true;
workBook.SaveCopyAs(saveFileName);
}
catch(Exception ex)
{
MessageBox.Show("Error, file is already opened!\n" + ex.Message);
}
}
xlApp.Quit();
GC.Collect();
MessageBox.Show("File : " + fileName + ".xls saved.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);

return true;
}
//CellFormatting Event
private void mgrdContentDatabase_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if(this.mgrdContentDatabase.Columns[e.ColumnIndex].HeaderText== "Size(GB)")
{
if (e.Value != null)
{
CovertFileSize(e);
}
}
}
//convert to file size
private void CovertFileSize(DataGridViewCellFormattingEventArgs formatting)
{
if (formatting.Value != null)
{
try
{
long bytes;
bytes = Convert.ToInt64(formatting.Value);
string size = "0 Bytes";

//GB
if (bytes >= 1073741824.0)
size = String.Format("{0:##.##}", bytes / 1073741824.0) + " GB";
//MB
else if (bytes >= 1048576.0)
size = String.Format("{0:##.##}", bytes / 1048576.0) + " MB";
//KB
else if (bytes >= 1024.0)
size = String.Format("{0:##.##}", bytes / 1024.0) + " KB";
//Bytes
else if (bytes > 0 && bytes < 1024.0)
size = bytes.ToString() + " Bytes";

formatting.Value = size;
formatting.FormattingApplied = true;
}
catch(FormatException)
{
formatting.FormattingApplied = false;
}
}

}

我想将转换后的数据导出到excel。

请帮助我如何修复或添加我的代码..

谢谢

最佳答案

您应该使用 FormattedValue 细胞属性:

string value = string.Format("{0}" , dataGridView1.Rows[r].Cells[i].FormattedValue);

关于c# - 如何将 datagridview 单元格格式值导出到 excel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40012258/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com