using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using HotelPms.Share.Util; using System; using System.Collections.Generic; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Text; namespace HotelPms.Share.Windows.Report { public class GridCsv : ReportBase, IDisposable, IReport { #region ★★★★★ Declartions ★★★★★ private bool m_Disposed = false; #endregion #region ★★★★★ Property ★★★★★ #endregion #region ★★★★★ Class Event ★★★★★ public GridCsv(DataTable data, GridStyle style) { m_Data = data; m_Style = style; } ~GridCsv() { Dispose(false); } protected virtual void Dispose(bool disposing) { if (!m_Disposed) //一回だけ { if (disposing) { //Managed Resources } //Unmanaged resources m_Disposed = true; } } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } #endregion #region ★★★★★ Control Event ★★★★★ #endregion #region ★★★★★ Private Function ★★★★★ #endregion #region ★★★★★ Public Function ★★★★★ public void Output() { string path = System.IO.Path.GetTempFileName().Replace(".tmp", ".csv"); Output(path); Process.Start(path); } public void Output(string file) { try { using (StreamWriter sw = new StreamWriter(file, false, Encoding.UTF8)) { StringBuilder colNames = new StringBuilder(); int col = 0; foreach (ColumnStyle cs in m_Style.ColumnStyle) { if (col > 0) { colNames.Append(","); } colNames.Append("\""); colNames.Append(cs.Text); colNames.Append("\""); col++; } sw.WriteLine(colNames.ToString()); //データ for (int i = 0; i < m_Data.Rows.Count; i++) { StringBuilder row = new StringBuilder(); col = 0; foreach (ColumnStyle cs in m_Style.ColumnStyle) { string text = m_Data.Rows[i][cs.DataField].ToString(); if (col > 0) { row.Append(","); } row.Append("\""); row.Append(text); row.Append("\""); col++; } sw.WriteLine(row.ToString()); } sw.Close(); } } catch { } } #endregion } }