/// <summary> /// CSV出力ボタン押下時処理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void CSVOutPut_Click(object sender, RoutedEventArgs e) { if (SearchResult == null) { ErrorMessage = "検索を行ってから出力ボタンを押下してください。"; return; } WinForms.SaveFileDialog sfd = new WinForms.SaveFileDialog(); // はじめに表示されるフォルダを指定する sfd.InitialDirectory = @"C:\"; // [ファイルの種類]に表示される選択肢を指定する sfd.Filter = "CSVファイル(*.csv)|*.csv|すべてのファイル(*.*)|*.*"; // 「CSVファイル」が選択されているようにする sfd.FilterIndex = 1; // タイトルを設定する sfd.Title = "保存先のファイルを選択してください"; // ダイアログボックスを閉じる前に現在のディレクトリを復元するようにする sfd.RestoreDirectory = true; if (sfd.ShowDialog() == WinForms.DialogResult.OK) { // CSVファイル出力 CSVData.SaveCSV(SearchResult, sfd.FileName, true, true, false, ','); MessageBox.Show("CSVファイルの出力が完了しました。"); } }
/// <summary> /// F5 リボン CSV出力 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public override void OnF5Key(object sender, KeyEventArgs e) { if (this.SearchResult == null || this.SearchResult.Rows.Count == 0) { MessageBox.Show("検索データがありません。"); return; } WinForms.SaveFileDialog sfd = new WinForms.SaveFileDialog(); // はじめに表示されるフォルダを指定する sfd.InitialDirectory = @"C:\"; // [ファイルの種類]に表示される選択肢を指定する sfd.Filter = "CSVファイル(*.csv)|*.csv|すべてのファイル(*.*)|*.*"; // 「CSVファイル」が選択されているようにする sfd.FilterIndex = 1; // タイトルを設定する sfd.Title = "保存先のファイルを選択してください"; // ダイアログボックスを閉じる前に現在のディレクトリを復元するようにする sfd.RestoreDirectory = true; if (sfd.ShowDialog() == WinForms.DialogResult.OK) { // CSVファイル出力 CSVData.SaveCSV(SearchResult, sfd.FileName, true, true, false, ','); MessageBox.Show("CSVファイルの出力が完了しました。"); } }
/// <summary> /// CSVデータの出力をおこなう /// </summary> /// <param name="tbl"></param> private void OutputCsv(DataTable tbl) { if (tbl == null || tbl.Rows.Count == 0) { this.ErrorMessage = "出力対象のデータがありません。"; return; } // CSV用データを取得 DataTable CSVデータ = CreateStockTakingCsv(tbl); WinForms.SaveFileDialog sfd = new WinForms.SaveFileDialog(); // はじめに表示されるフォルダを指定する sfd.InitialDirectory = @"C:\"; // [ファイルの種類]に表示される選択肢を指定する sfd.Filter = "CSVファイル(*.csv)|*.csv|すべてのファイル(*.*)|*.*"; // 「CSVファイル」が選択されているようにする sfd.FilterIndex = 1; // タイトルを設定する sfd.Title = "保存先のファイルを選択してください"; // ダイアログボックスを閉じる前に現在のディレクトリを復元するようにする sfd.RestoreDirectory = true; if (sfd.ShowDialog() == WinForms.DialogResult.OK) { // CSVファイル出力 CSVData.SaveCSV(CSVデータ, sfd.FileName, true, true, false, ','); MessageBox.Show("CSVファイルの出力が完了しました。"); } }
/// <summary> /// CSVファイル出力 /// </summary> /// <param name="tbl"></param> private void OutPutCSV(DataTable tbl) { if (tbl.Rows.Count < 1) { this.ErrorMessage = "対象データが存在しません。"; return; } WinForms.SaveFileDialog sfd = new WinForms.SaveFileDialog(); // はじめに表示されるフォルダを指定する sfd.InitialDirectory = @"C:\"; // [ファイルの種類]に表示される選択肢を指定する sfd.Filter = "CSVファイル(*.csv)|*.csv|すべてのファイル(*.*)|*.*"; // 「CSVファイル」が選択されているようにする sfd.FilterIndex = 1; // タイトルを設定する sfd.Title = "保存先のファイルを選択してください"; // ダイアログボックスを閉じる前に現在のディレクトリを復元するようにする sfd.RestoreDirectory = true; if (sfd.ShowDialog() == WinForms.DialogResult.OK) { tbl.Columns.Remove("得意先枝番"); // No.223 Add // CSVファイル出力 CSVData.SaveCSV(tbl, sfd.FileName, true, true, false, ','); MessageBox.Show("CSVファイルの出力が完了しました。"); } }
/// <summary> /// CSVデータの出力をおこなう /// </summary> /// <param name="tbl"></param> private void outputCsv(DataTable tbl) { if (tbl == null || tbl.Rows.Count == 0) { MessageBox.Show("出力対象のデータがありません。"); return; } // CSV出力用に列名を編集する changeColumnsName(tbl); // Np.398 Mod WinForms.SaveFileDialog sfd = new WinForms.SaveFileDialog(); // はじめに表示されるフォルダを指定する sfd.InitialDirectory = @"C:\"; // [ファイルの種類]に表示される選択肢を指定する sfd.Filter = "CSVファイル(*.csv)|*.csv|すべてのファイル(*.*)|*.*"; // 「CSVファイル」が選択されているようにする sfd.FilterIndex = 1; // タイトルを設定する sfd.Title = "保存先のファイルを選択してください"; // ダイアログボックスを閉じる前に現在のディレクトリを復元するようにする sfd.RestoreDirectory = true; if (sfd.ShowDialog() == WinForms.DialogResult.OK) { // CSVファイル出力 CSVData.SaveCSV(tbl, sfd.FileName, true, true, false, ','); MessageBox.Show("CSVファイルの出力が完了しました。"); } }
/// <summary> /// F5 リボン CSV出力 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public override void OnF5Key(object sender, KeyEventArgs e) { if (this.SearchResult == null) { return; } if (this.SearchResult.Rows.Count == 0) { this.ErrorMessage = "印刷データがありません。"; return; } if (this.spGridList.ActiveCellPosition.Row < 0) { MessageBox.Show("検索データがありません。"); return; } WinForms.SaveFileDialog sfd = new WinForms.SaveFileDialog(); // はじめに表示されるフォルダを指定する sfd.InitialDirectory = @"C:\"; // [ファイルの種類]に表示される選択肢を指定する sfd.Filter = "CSVファイル(*.csv)|*.csv|すべてのファイル(*.*)|*.*"; // 「CSVファイル」が選択されているようにする sfd.FilterIndex = 1; // タイトルを設定する sfd.Title = "保存先のファイルを選択してください"; // ダイアログボックスを閉じる前に現在のディレクトリを復元するようにする sfd.RestoreDirectory = true; if (sfd.ShowDialog() == WinForms.DialogResult.OK) { // 不要な行を削除する DataTable csvDt = SearchResult.Copy(); csvDt.Columns.Remove("str品番コード"); // CSVファイル出力 CSVData.SaveCSV(csvDt, sfd.FileName, true, true, false, ',', true); MessageBox.Show("CSVファイルの出力が完了しました。"); } }
/// <summary> /// CSVファイル出力 /// </summary> /// <param name="tbl"></param> private void OutPutCSV(DataTable tbl) { WinForms.SaveFileDialog sfd = new WinForms.SaveFileDialog(); // はじめに表示されるフォルダを指定する sfd.InitialDirectory = @"C:\"; // [ファイルの種類]に表示される選択肢を指定する sfd.Filter = "CSVファイル(*.csv)|*.csv|すべてのファイル(*.*)|*.*"; // 「CSVファイル」が選択されているようにする sfd.FilterIndex = 1; // タイトルを設定する sfd.Title = "保存先のファイルを選択してください"; // ダイアログボックスを閉じる前に現在のディレクトリを復元するようにする sfd.RestoreDirectory = true; if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { //CSVファイル出力 CSVData.SaveCSV(tbl, sfd.FileName, true, true, false, ','); MessageBox.Show("CSVファイルの出力が完了しました。"); } }
/// <summary> /// CSVファイル出力 /// </summary> /// <param name="tbl"></param> private void OutPutCSV(DataTable tbl) { if (tbl.Rows.Count < 1) { this.ErrorMessage = "対象データが存在しません。"; return; } WinForms.SaveFileDialog sfd = new WinForms.SaveFileDialog(); // はじめに表示されるフォルダを指定する sfd.InitialDirectory = @"C:\"; // [ファイルの種類]に表示される選択肢を指定する sfd.Filter = "CSVファイル(*.csv)|*.csv|すべてのファイル(*.*)|*.*"; // 「CSVファイル」が選択されているようにする sfd.FilterIndex = 1; // タイトルを設定する sfd.Title = "保存先のファイルを選択してください"; // ダイアログボックスを閉じる前に現在のディレクトリを復元するようにする sfd.RestoreDirectory = true; if (sfd.ShowDialog() == WinForms.DialogResult.OK) { // カラム名を変更 tbl.Columns["通常税率対象支払額"].ColumnName = "仕入(通常)"; tbl.Columns["軽減税率対象支払額"].ColumnName = "仕入(軽減)"; tbl.Columns["非課税支払額"].ColumnName = "仕入(非課税)"; tbl.Columns["通常税消費税"].ColumnName = "消費税(通常)"; tbl.Columns["軽減税消費税"].ColumnName = "消費税(軽減)"; tbl.Columns["税込支払額"].ColumnName = "通常分計"; tbl.Columns["軽減税込支払額"].ColumnName = "軽減分計"; tbl.Columns["当月支払額"].ColumnName = "当月仕入額"; // CSVファイル出力 CSVData.SaveCSV(tbl, sfd.FileName, true, true, false, ','); MessageBox.Show("CSVファイルの出力が完了しました。"); } }
/// <summary> /// Excel出力 /// </summary> /// <param name="dt"></param> private void OutputExcel(DataTable dt) { // Excel起動 Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook excelWb = ExcelApp.Workbooks.Add(); Microsoft.Office.Interop.Excel.Worksheet excelWs = excelWb.Sheets[1]; Microsoft.Office.Interop.Excel.Range xlCellsTo = null; // セル終点(中継用) Microsoft.Office.Interop.Excel.Range xlRangeTo = null; // セル終点 Microsoft.Office.Interop.Excel.Range xlTargetRange = null; // 出力対象レンジ // Excelウインドウ非表示 ExcelApp.Visible = false; // EXCEL出力処理 try { base.SetBusyForInput(); excelWs.Select(Type.Missing); // 読み込む範囲を指定する object[,] values = new object[dt.Rows.Count + 1, dt.Columns.Count]; // ヘッダカラム設定 DataRow columnRow = dt.Rows[0]; for (int i = 0; i < columnRow.Table.Columns.Count; i++) { values[0, i] = columnRow.Table.Columns[i].Caption; } // 明細部設定 int cnt = 1; foreach (DataRow row in dt.Rows) { values[cnt, 0] = row.Field <int>("締年月").ToString(); values[cnt, 1] = row.Field <int>("品番コード").ToString(); values[cnt, 2] = row.Field <string>("自社品番"); values[cnt, 3] = row.Field <string>("自社色"); values[cnt, 4] = row.Field <string>("自社品名"); values[cnt, 5] = row.Field <string>("色名称"); values[cnt, 6] = row.Field <DateTime?>("賞味期限") == null ? null : row.Field <DateTime?>("賞味期限").Value.ToShortDateString(); values[cnt, 7] = row.Field <int>("倉庫コード").ToString(); values[cnt, 8] = row.Field <string>("倉庫名"); values[cnt, 9] = row.Field <decimal?>("仕入単価").ToString(); values[cnt, 10] = row.Field <int>("在庫数量").ToString(); values[cnt, 11] = row.Field <decimal?>("調整単価"); values[cnt, 12] = row.Field <decimal?>("調整数量"); cnt++; } //ヘッダのみ色を変更 xlTargetRange = excelWs.Range["A1", "M1"]; xlTargetRange.Interior.Color = System.Drawing.Color.FromArgb(135, 231, 173); // 終点セル取得 xlCellsTo = excelWs.Cells; xlRangeTo = xlCellsTo[dt.Rows.Count + 1, columnRow.Table.Columns.Count] as Microsoft.Office.Interop.Excel.Range; // 対象レンジ設定 xlTargetRange = excelWs.Range["A1", xlRangeTo]; // データセット xlTargetRange.Value = values; // カラム幅セット xlTargetRange.Columns.AutoFit(); // 罫線セット xlTargetRange.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous; xlTargetRange.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous; xlTargetRange.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous; xlTargetRange.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous; xlTargetRange.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous; xlTargetRange.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous; // 書式を文字列に設定 //xlTargetRange.NumberFormatLocal = "@"; // 書式を数字に設定 //xlTargetRange = excelWs.Range["J1", xlRangeTo]; //xlTargetRange.NumberFormatLocal = "0.00"; #region 前を付けて保存Dialogを出力 WinForms.SaveFileDialog sfd = new WinForms.SaveFileDialog(); // はじめに表示されるフォルダを指定する //sfd.InitialDirectory = @"C:\"; // [ファイルの種類]に表示される選択肢を指定する sfd.Filter = "Excelファイル(*.xlsx)|*.xlsx"; // 「CSVファイル」が選択されているようにする sfd.FilterIndex = 1; // タイトルを設定する sfd.Title = "保存先のファイルを選択してください"; // ダイアログボックスを閉じる前に現在のディレクトリを復元するようにする sfd.RestoreDirectory = true; #endregion base.SetFreeForInput(); if (sfd.ShowDialog() == WinForms.DialogResult.OK) { base.SetBusyForInput(); ExcelApp.DisplayAlerts = false; // (Excelアプリ)上書きアラート false // EXCEL出力 excelWb.SaveAs(sfd.FileName); base.SetFreeForInput(); MessageBox.Show("EXCELファイルの出力が完了しました。"); } excelWb.Close(false); ExcelApp.Quit(); } catch (Exception ex) { base.SetFreeForInput(); MessageBox.Show(ex.Message); } finally { //オブジェクト解放 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlCellsTo); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlRangeTo); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlTargetRange); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWs); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWb); System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp); } }