成达修改20230724
This commit is contained in:
@@ -546,8 +546,72 @@ namespace BLL.Common
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public static string DataSetToExcel(DataTable dt, string Path)
|
||||
{
|
||||
var result = string.Empty;
|
||||
FileStream fs = null;
|
||||
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||
|
||||
private static string GetValue(string cellValue, string type)
|
||||
|
||||
XSSFSheet sheet = (XSSFSheet)workbook.CreateSheet(dt.TableName);
|
||||
|
||||
XSSFCellStyle dateStyle = (XSSFCellStyle)workbook.CreateCellStyle();
|
||||
XSSFDataFormat format = (XSSFDataFormat)workbook.CreateDataFormat();
|
||||
dateStyle.DataFormat = format.GetFormat("yyyy-mm-dd");
|
||||
|
||||
int rowIndex = 0;
|
||||
|
||||
#region 新建表,填充表头,填充列头,样式
|
||||
if (rowIndex == 0)
|
||||
{
|
||||
//#region 列头及样式
|
||||
//{
|
||||
// XSSFRow headerRow = (XSSFRow)sheet.CreateRow(0);
|
||||
// XSSFCellStyle headStyle = (XSSFCellStyle)workbook.CreateCellStyle();
|
||||
// //headStyle.Alignment = CellHorizontalAlignment.CENTER;
|
||||
// XSSFFont font = (XSSFFont)workbook.CreateFont();
|
||||
// font.FontHeightInPoints = 10;
|
||||
// font.Boldweight = 700;
|
||||
// headStyle.SetFont(font);
|
||||
//}
|
||||
//#endregion
|
||||
//填充表头
|
||||
XSSFRow dataRow = (XSSFRow)sheet.CreateRow(rowIndex);
|
||||
foreach (DataColumn column in dt.Columns)
|
||||
{
|
||||
dataRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
|
||||
}
|
||||
|
||||
rowIndex = 1;
|
||||
}
|
||||
#endregion
|
||||
|
||||
foreach (DataRow row in dt.Rows)
|
||||
{
|
||||
XSSFRow dataRow = (XSSFRow)sheet.CreateRow(rowIndex);
|
||||
|
||||
#region 填充内容
|
||||
foreach (DataColumn column in dt.Columns)
|
||||
{
|
||||
XSSFCell newCell = (XSSFCell)dataRow.CreateCell(column.Ordinal);
|
||||
string type = row[column].GetType().FullName.ToString();
|
||||
newCell.SetCellValue(GetValue(row[column].ToString(), type));
|
||||
}
|
||||
#endregion
|
||||
|
||||
rowIndex++;
|
||||
}
|
||||
|
||||
|
||||
using (fs = File.OpenWrite(Path))
|
||||
{
|
||||
workbook.Write(fs);//向打开的这个xls文件中写入数据
|
||||
result = Path;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static string GetValue(string cellValue, string type)
|
||||
{
|
||||
object value = string.Empty;
|
||||
switch (type)
|
||||
|
||||
Reference in New Issue
Block a user