我有一个DataGridView,其中包含红色,蓝色等几行。我需要将具有此精确格式的DataGridView导出到Excel工作表,我的代码写在下面但是不起作用请帮助我。
SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog() == DialogResult.OK)
{
string stOutput = "";
// Export titles:
string sHeaders = "";
for (int j = 0; j < dgvImportData.Columns.Count; j++)
sHeaders = sHeaders.ToString() + Convert.ToString(dgvImportData.Columns[j].HeaderText) + "\t";
stOutput += sHeaders + "\r\n";
// Export data.
if (Status == 1)
{
for (int i = 0; i < dgvImportData.RowCount; i++)
{
if (Convert.ToInt32(dgvImportData.Rows[i].Cells["Status"].Value) == Status)
{
string stLine = "";
for (int j = 0; j < dgvImportData.Rows[i].Cells.Count; j++)
stLine = stLine.ToString() + Convert.ToString(dgvImportData.Rows[i].Cells[j].Value) + "\t";
stOutput += stLine + "\r\n";
}
}
}
else
{
for (int i = 0; i < dgvImportData.RowCount; i++)
{
if (Convert.ToInt32(dgvImportData.Rows[i].Cells["Status"].Value) == Status)
{
string stLine = "";
for (int j = 0; j < dgvImportData.Rows[i].Cells.Count; j++)
stLine = stLine.ToString() + Convert.ToString(dgvImportData.Rows[i].Cells[j].Value) + "\t";
stOutput += stLine + "\r\n";
}
}
}
Encoding utf16 = Encoding.GetEncoding(1254);
byte[] output = utf16.GetBytes(stOutput);
FileStream fs = new FileStream(sfd.FileName, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(output, 0, output.Length); //write the encoded file
bw.Flush();
bw.Close();
fs.Close();
答案 0 :(得分:0)
将Microsoft.Office.Interop.Excel.dll添加到项目中。
添加以下using语句:
using Excel = Microsoft.Office.Interop.Excel;
现在添加以下函数将整数转换为Excel列字母:
private string GetExcelColumnName(int columnNumber)
{
int dividend = columnNumber;
string columnName = String.Empty;
int modulo;
while (dividend > 0)
{
modulo = (dividend - 1) % 26;
columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
dividend = (int)((dividend - modulo) / 26);
}
return columnName;
}
然后添加以下功能以继续导出
private void ExportToExcel(DataGridView dv)
{
Excel.Application xlApp;
xlApp = new Excel.Application();
xlApp.Visible = true;
xlApp.AskToUpdateLinks = false;
xlApp.DisplayAlerts = false;
Excel.Workbook wb = (Excel.Workbook)xlApp.Workbooks.Add();
Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets.Add();
int rowstartindex = 1;
//Create headers
for (int i = 0; i < dv.Columns.Count; i++)
{
Excel.Range CellHeadersRange = ws.get_Range(GetExcelColumnName(i+1) + rowstartindex.ToString(), GetExcelColumnName(i+1) + rowstartindex.ToString());
CellHeadersRange.Value = dv.Columns[i].HeaderText;
}
//Write data
for (int i = 0; i < dv.Rows.Count; i++)
{
for (int j = 0; j < dv.Columns.Count; j++)
{
Excel.Range CellDataRange = ws.get_Range(GetExcelColumnName(j+1) + (i+rowstartindex+1).ToString());
CellDataRange.Value = dv[j, i].Value;
//Verify that backgroundcolor of datagrid is not RGB(0,0,0,0) and in that case apply datagridviewcell color to excel range
if(!dv.Rows[i].Cells[j].Style.BackColor.IsEmpty)
CellDataRange.Interior.Color = dv.Rows[i].Cells[j].Style.BackColor;
//Verify that font style exist before checking for bold and in that case apply datagridviewcell font.bold property to excel range
if(dv.Rows[i].Cells[j].Style.Font != null)
CellDataRange.Font.Bold = dv.Rows[i].Cells[j].Style.Font.Bold;
}
}
wb = null;
ws = null;
xlApp = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
一旦您理解了这里的逻辑,就可以轻松地将您的datagridview的更多样式/格式应用于excel范围。