public void ExportDataToExcel(DataTable TableName, string FileName)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
//设置文件标题
saveFileDialog.Title = "导出Excel文件";
//设置文件类型
saveFileDialog.Filter = "Excel 工作簿(*.xlsx)|*.xlsx|Excel 97-2003 工作簿(*.xls)|*.xls";
//设置默认文件类型显示顺序
saveFileDialog.FilterIndex = 1;
//是否自动在文件名中添加扩展名
saveFileDialog.AddExtension = true;
//是否记忆上次打开的目录
saveFileDialog.RestoreDirectory = true;
//设置默认文件名
saveFileDialog.FileName = FileName;
//按下确定选择的按钮
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
//获得文件路径
string localFilePath = saveFileDialog.FileName.ToString();
//NPOI
IWorkbook workbook;
string FileExt = Path.GetExtension(localFilePath).ToLower();
if (FileExt == ".xlsx")
{
workbook = new XSSFWorkbook();
}
else if (FileExt == ".xls")
{
workbook = new HSSFWorkbook();
}
else
{
workbook = null;
}
if (workbook == null)
{
return;
}
ISheet sheet = string.IsNullOrEmpty(FileName) ? workbook.CreateSheet("Sheet1") : workbook.CreateSheet(FileName);
try
{
//读取标题
IRow rowHeader = sheet.CreateRow(0);
for (int i = 1; i < TableName.Columns.Count; i++)
{
int columnNum = i - 1;
ICell cell = rowHeader.CreateCell(columnNum);
ICellStyle style = workbook.CreateCellStyle();
style.FillForegroundColor = HSSFColor.Grey25Percent.Index;//25%的灰色背景
style.FillPattern = FillPattern.SolidForeground;
string title = GetName(TableName.Columns[i].ColumnName);
cell.SetCellValue(title);
cell.CellStyle = style;
int columnWidth = sheet.GetColumnWidth(columnNum) / 256;//获取当前列宽度
int length = Encoding.UTF8.GetBytes(title.ToString()).Length;//获取当前单元格的内容宽度
if (columnWidth < length + 1)
{
columnWidth = length + 1;
}//若当前单元格内容宽度大于列宽,则调整列宽为当前单元格宽度,后面的+1是我人为的将宽度增加一个字符
sheet.SetColumnWidth(columnNum, columnWidth * 256);
}
//读取数据
for (int i = 0; i < TableName.Rows.Count; i++)
{
IRow rowData = sheet.CreateRow(i + 1);
for (int j = 1; j < TableName.Columns.Count; j++)
{
ICell cell = rowData.CreateCell(j - 1);
cell.SetCellValue(TableName.Rows[i][j].ToString());
//int columnNum = j - 1;
//int columnWidth = sheet.GetColumnWidth(columnNum) / 256;//获取当前列宽度
//for (int rowNum = 1; rowNum <= sheet.LastRowNum; rowNum++)//在这一列上循环行
//{
// IRow currentRow = sheet.GetRow(rowNum);
// ICell currentCell = currentRow.GetCell(columnNum);
// int length = Encoding.UTF8.GetBytes(currentCell.ToString()).Length;//获取当前单元格的内容宽度
// if (columnWidth < length + 1)
// {
// columnWidth = length + 1;
// }//若当前单元格内容宽度大于列宽,则调整列宽为当前单元格宽度,后面的+1是我人为的将宽度增加一个字符
//}
//sheet.SetColumnWidth(columnNum, columnWidth * 256);
}
}
//转为字节数组
MemoryStream stream = new MemoryStream();
workbook.Write(stream);
var buf = stream.ToArray();
//保存为Excel文件
using (FileStream fs = new FileStream(localFilePath, FileMode.Create, FileAccess.Write))
{
fs.Write(buf, 0, buf.Length);
fs.Flush();
fs.Close();
}
//成功提示
if (UIHelper.MessageBoxQuestion("导出成功,是否立即打开?"))
{
System.Diagnostics.Process.Start(localFilePath);
}
}
catch (Exception ex)
{
UIHelper.MessageBoxError(ex.Message);
}
finally
{ }
}
}
C# NPIO导出Excel设置宽度背景色
于 2021-09-30 16:37:20 首次发布