成达修改20230724

This commit is contained in:
2023-07-24 16:47:13 +08:00
parent dc33ea0366
commit f0b0eca670
28 changed files with 794 additions and 699 deletions
+65 -1
View File
@@ -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)