|
|
|
C# รบกวนสอบถามการใช้งาน EPplus สำหรับสร้าง Excel หน่อยนะครับ ^ ^" |
|
|
|
|
|
|
|
EPPlus อ่านได้ เขียน ได้
แต่ทำงานได้เฉพาะ XLSX T___T
เท่าที่เคยใช้นิด ๆ หน่อย ๆ
ผมมีปัญหาอ่านค่า 0 แล้วมันมาเป็นช่องว่างแทน ^___^"
|
|
|
|
|
Date :
2013-07-04 13:12:24 |
By :
fonfire |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
แย่จัง T___T
ขอขอบคุณมากนะครับ สำหรับคำตอบ ^ ^"
|
|
|
|
|
Date :
2013-07-04 13:21:06 |
By :
Spada_555 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ว่าจะไม่ตอบห้องนี้แล้วนะ
เป็นครั้งสุดท้ายแล้วกัน
ปกติเราจะอ่าน excel ด้วย excel interop
เพราะเร็วกว่า กิน memory น้อยกว่า (เราอ่านทีเป็นแสน reccord เห็นความแตกต่างชัดเจน)
แต่ถ้าเขียนเราใช้ epplus
โค้ดด้านล่างใช้ dataset datatable list of dataset ออกเป็น excel
ลองเล่นดู แต่ละ mothod แล้วกัน
ส่วนเจอค่า 0 แล้วเป็นค่าว่างไม่เคยเจอ
ปล. ตัวล่างเป็นของ win app ถ้าจะใช้กับ web ต้องเปลี่ยนนิดหน่อย
จาก write ออกเป็น file เปลี่ยนให้มันส่งออกเป็น stream
Epplus.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using OfficeOpenXml;
using OfficeOpenXml.Style;
namespace ECIM.Export
{
public partial class Epplus
{
private string _filePath;
public Epplus(string FilePath)
{
_filePath = FilePath;
}
#region ===================== Export =======================
public bool Export(DataSet DsData)
{
int i = 1;
ExcelPackage xlPackage = new ExcelPackage();
foreach (DataTable DtData in DsData.Tables)
{
string sheetName = (DtData.TableName != string.Empty) ? DtData.TableName : string.Format("Sheet{0}", i.ToString());
AddSheet(xlPackage, sheetName, DtData);
i++;
}
return Save(xlPackage.GetAsByteArray());
}
public bool Export(DataTable DtData)
{
ExcelPackage xlPackage = new ExcelPackage();
string sheetName = (DtData.TableName != string.Empty) ? DtData.TableName : "Sheet1";
AddSheet(xlPackage, sheetName, DtData);
return Save(xlPackage.GetAsByteArray());
}
public bool Export(DataSet DsData, string TableName)
{
return Export(DsData.Tables[TableName]);
}
public bool ExportWithSum(List<DataSet> DataList)
{
int i = 1;
ExcelPackage xlPackage = new ExcelPackage();
foreach (DataSet DsData in DataList)
{
string sheetName = (DsData.DataSetName != string.Empty) ? DsData.DataSetName : string.Format("Sheet{0}", i.ToString());
AddSheet(xlPackage, sheetName, DsData, true);
i++;
}
return Save(xlPackage.GetAsByteArray());
}
public bool ExportWithSum(DataSet DsData)
{
int i = 1;
ExcelPackage xlPackage = new ExcelPackage();
foreach (DataTable DtData in DsData.Tables)
{
string sheetName = (DtData.TableName != string.Empty) ? DtData.TableName : string.Format("Sheet{0}", i.ToString());
AddSheet(xlPackage, sheetName, DtData, true);
i++;
}
return Save(xlPackage.GetAsByteArray());
}
public bool ExportWithSum(DataTable DtData)
{
ExcelPackage xlPackage = new ExcelPackage();
string sheetName = (DtData.TableName != string.Empty) ? DtData.TableName : "Sheet1";
AddSheet(xlPackage, sheetName, DtData, true);
return Save(xlPackage.GetAsByteArray());
}
public bool ExportWithSum(DataSet DsData, string TableName)
{
return ExportWithSum(DsData.Tables[TableName]);
}
protected void AddSheet(ExcelPackage xlPackage, string SheetName, DataSet DsData)
{
AddSheet(xlPackage, SheetName, DsData, false);
}
protected void AddSheet(ExcelPackage xlPackage, string SheetName, DataSet DsData, bool SumData)
{
ExcelWorksheet xlSheet = xlPackage.Workbook.Worksheets.Add(SheetName);
int startRow = 1;
foreach (DataTable DtData in DsData.Tables)
{
FillData(xlSheet, DtData, startRow, SumData, DtData.TableName);
startRow += DtData.Rows.Count + 3 + ((SumData) ? 1 : 0);
}
}
protected void AddSheet(ExcelPackage xlPackage, string SheetName, DataTable DtData)
{
AddSheet(xlPackage, SheetName, DtData, false);
}
protected void AddSheet(ExcelPackage xlPackage, string SheetName, DataTable DtData, bool SumData)
{
ExcelWorksheet xlSheet = xlPackage.Workbook.Worksheets.Add(SheetName);
FillData(xlSheet, DtData, 1, SumData);
}
protected void FillData(ExcelWorksheet xlSheet, DataTable DtData, int StartRow)
{
FillData(xlSheet, DtData, StartRow, false);
}
protected void FillData(ExcelWorksheet xlSheet, DataTable DtData, int StartRow, bool SumData)
{
FillData(xlSheet, DtData, StartRow, SumData, string.Empty);
}
protected void FillData(ExcelWorksheet xlSheet, DataTable DtData, int StartRow, bool SumData, string HeaderName)
{
int headerLength = (HeaderName != string.Empty) ? 1 : 0;
int rowCount = DtData.Rows.Count;
int colomnCount = DtData.Columns.Count;
if (HeaderName != string.Empty)
{
xlSheet.Cells[StartRow, 1].Value = HeaderName;
xlSheet.Cells[StartRow, 1, StartRow, colomnCount].Merge = true;
xlSheet.Cells[StartRow, 1, StartRow, colomnCount].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
//font
xlSheet.Cells[StartRow, 1, StartRow, colomnCount].Style.Font.SetFromFont(new System.Drawing.Font("MS Sans Serif", 10, System.Drawing.FontStyle.Bold));
//background color
xlSheet.Cells[StartRow, 1, StartRow, colomnCount].Style.Fill.PatternType = ExcelFillStyle.Solid;
xlSheet.Cells[StartRow, 1, StartRow, colomnCount].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightYellow);
//border
xlSheet.Cells[StartRow, 1, StartRow, colomnCount].Style.Border.Top.Style = ExcelBorderStyle.Thin;
xlSheet.Cells[StartRow, 1, StartRow, colomnCount].Style.Border.Right.Style = ExcelBorderStyle.Thin;
xlSheet.Cells[StartRow, 1, StartRow, colomnCount].Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
xlSheet.Cells[StartRow, 1, StartRow, colomnCount].Style.Border.Left.Style = ExcelBorderStyle.Thin;
xlSheet.Cells[StartRow, 1, StartRow, colomnCount].Style.Border.Top.Color.SetColor(System.Drawing.Color.LightGray);
xlSheet.Cells[StartRow, 1, StartRow, colomnCount].Style.Border.Right.Color.SetColor(System.Drawing.Color.LightGray);
xlSheet.Cells[StartRow, 1, StartRow, colomnCount].Style.Border.Bottom.Color.SetColor(System.Drawing.Color.LightGray);
xlSheet.Cells[StartRow, 1, StartRow, colomnCount].Style.Border.Left.Color.SetColor(System.Drawing.Color.LightGray);
}
xlSheet.Cells[StartRow + headerLength, 1].LoadFromDataTable(DtData, true);
IEnumerable<int> dateColumns = from DataColumn d in DtData.Columns
where d.DataType == typeof(DateTime) || d.ColumnName.Contains("Date")
orderby d.Ordinal ascending
select d.Ordinal + 1;
IEnumerable<int> doubleColumn = from DataColumn d in DtData.Columns
where d.DataType == typeof(double) || d.DataType == typeof(float)
orderby d.Ordinal ascending
select d.Ordinal + 1;
if (DtData.Rows.Count > 0)
{
foreach (int dc in dateColumns)
{
xlSheet.Cells[StartRow + headerLength + 1, dc, StartRow + headerLength + rowCount, dc].Style.Numberformat.Format = "dd/MM/yyyy";
}
foreach (int dc in doubleColumn)
{
xlSheet.Cells[StartRow + headerLength + 1, dc, StartRow + headerLength + rowCount, dc].Style.Numberformat.Format = "#,##0.00";
}
//font
xlSheet.Cells[StartRow + headerLength, 1, StartRow + headerLength + rowCount, colomnCount].Style.Font.SetFromFont(new System.Drawing.Font("MS Sans Serif", 10, System.Drawing.FontStyle.Regular));
//background color
xlSheet.Cells[StartRow + headerLength, 1, StartRow + headerLength, colomnCount].Style.Fill.PatternType = ExcelFillStyle.Solid;
xlSheet.Cells[StartRow + headerLength, 1, StartRow + headerLength, colomnCount].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightYellow);
//border
xlSheet.Cells[StartRow + headerLength, 1, StartRow + headerLength + rowCount, colomnCount].Style.Border.Top.Style = ExcelBorderStyle.Thin;
xlSheet.Cells[StartRow + headerLength, 1, StartRow + headerLength + rowCount, colomnCount].Style.Border.Right.Style = ExcelBorderStyle.Thin;
xlSheet.Cells[StartRow + headerLength, 1, StartRow + headerLength + rowCount, colomnCount].Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
xlSheet.Cells[StartRow + headerLength, 1, StartRow + headerLength + rowCount, colomnCount].Style.Border.Left.Style = ExcelBorderStyle.Thin;
xlSheet.Cells[StartRow + headerLength, 1, StartRow + headerLength + rowCount, colomnCount].Style.Border.Top.Color.SetColor(System.Drawing.Color.LightGray);
xlSheet.Cells[StartRow + headerLength, 1, StartRow + headerLength + rowCount, colomnCount].Style.Border.Right.Color.SetColor(System.Drawing.Color.LightGray);
xlSheet.Cells[StartRow + headerLength, 1, StartRow + headerLength + rowCount, colomnCount].Style.Border.Bottom.Color.SetColor(System.Drawing.Color.LightGray);
xlSheet.Cells[StartRow + headerLength, 1, StartRow + headerLength + rowCount, colomnCount].Style.Border.Left.Color.SetColor(System.Drawing.Color.LightGray);
if (SumData)
{
xlSheet.Cells[StartRow + headerLength + rowCount + 1, 1].Value = "รวม";
xlSheet.Cells[StartRow + headerLength + rowCount + 1, 1].Style.Font.SetFromFont(new System.Drawing.Font("MS Sans Serif", 10, System.Drawing.FontStyle.Bold));
xlSheet.Cells[StartRow + headerLength + rowCount + 1, 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
if (doubleColumn.First() > 1)
xlSheet.Cells[StartRow + headerLength + rowCount + 1, 1, StartRow + headerLength + rowCount + 1, doubleColumn.First() - 1].Merge = true;
foreach (int dc in doubleColumn)
{
xlSheet.Cells[StartRow + headerLength + rowCount + 1, dc, StartRow + headerLength + rowCount + 1, dc].Formula = string.Format("SUM({0}:{1})", xlSheet.Cells[StartRow + headerLength, dc].Address, xlSheet.Cells[StartRow + headerLength + rowCount, dc].Address);
xlSheet.Cells[StartRow + headerLength + rowCount + 1, dc, StartRow + headerLength + rowCount + 1, dc].Style.Numberformat.Format = "#,##0.00";
}
//background color
xlSheet.Cells[StartRow + headerLength + rowCount + 1, 1, StartRow + headerLength + rowCount + 1, colomnCount].Style.Fill.PatternType = ExcelFillStyle.Solid;
xlSheet.Cells[StartRow + headerLength + rowCount + 1, 1, StartRow + headerLength + rowCount + 1, colomnCount].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightYellow);
//border
xlSheet.Cells[StartRow + headerLength + rowCount + 1, 1, StartRow + headerLength + rowCount + 1, colomnCount].Style.Border.Top.Style = ExcelBorderStyle.Thin;
xlSheet.Cells[StartRow + headerLength + rowCount + 1, 1, StartRow + headerLength + rowCount + 1, colomnCount].Style.Border.Right.Style = ExcelBorderStyle.Thin;
xlSheet.Cells[StartRow + headerLength + rowCount + 1, 1, StartRow + headerLength + rowCount + 1, colomnCount].Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
xlSheet.Cells[StartRow + headerLength + rowCount + 1, 1, StartRow + headerLength + rowCount + 1, colomnCount].Style.Border.Left.Style = ExcelBorderStyle.Thin;
xlSheet.Cells[StartRow + headerLength + rowCount + 1, 1, StartRow + headerLength + rowCount + 1, colomnCount].Style.Border.Top.Color.SetColor(System.Drawing.Color.LightGray);
xlSheet.Cells[StartRow + headerLength + rowCount + 1, 1, StartRow + headerLength + rowCount + 1, colomnCount].Style.Border.Right.Color.SetColor(System.Drawing.Color.LightGray);
xlSheet.Cells[StartRow + headerLength + rowCount + 1, 1, StartRow + headerLength + rowCount + 1, colomnCount].Style.Border.Bottom.Color.SetColor(System.Drawing.Color.LightGray);
xlSheet.Cells[StartRow + headerLength + rowCount + 1, 1, StartRow + headerLength + rowCount + 1, colomnCount].Style.Border.Left.Color.SetColor(System.Drawing.Color.LightGray);
//Auto Fit Columns
xlSheet.Cells[StartRow, 1, StartRow + headerLength + rowCount + 1, colomnCount].AutoFitColumns();
}
}
}
protected bool Save(byte[] buffer)
{
bool result;
if (File.Exists(_filePath))
{
File.Delete(_filePath);
}
try
{
File.WriteAllBytes(_filePath, buffer);
result = true;
}
catch
{
result = false;
}
return result;
}
#endregion
}
}
|
|
|
|
|
Date :
2013-07-04 14:01:21 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณมากครับ ^ ^"
|
|
|
|
|
Date :
2013-07-04 14:25:48 |
By :
Spada_555 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|