Search

2019/02/23

C#.NETでListViewにクリップボード機能のコピーを追加:選択範囲のみCrt+Cでデータをクリップボードへ

タイトルのまま。
ListViewの選択範囲をクリップボードへコピー。
そのままエクセルに貼り付けします。

asのキャスティングはnullを返すのってこういうふうに使うものなのか。
C# Tips-キャストすべきかasするべきか-

あと、コラムヘッダーの設定方法を完全に忘れてた。
View=View.Detailsにしとかないとコラムヘッダーが表示されない。




stackoverflowを参考。

How can I Copy to clipboard, a ListView's multiple selected items?
https://stackoverflow.com/questions/30209283/how-can-i-copy-to-clipboard-a-listviews-multiple-selected-items

雑文ですみません。雑記的なブログです。
  1.         public MainForm()
  2.         {
  3.             //
  4.             // The InitializeComponent() call is required for Windows Forms designer support.
  5.             //
  6.             InitializeComponent();
  7.             
  8.             //コラムヘッダーの設定
  9.             this.lsvTEST.View = View.Details;
  10.             this.lsvTEST.GridLines = true;
  11.             this.lsvTEST.MultiSelect = true;
  12.             this.lsvTEST.FullRowSelect = true;
  13.             int maxCol = 3;
  14.             System.Windows.Forms.ColumnHeader[] colHd = new System.Windows.Forms.ColumnHeader[maxCol];
  15.             for(int i=0;i<maxCol;i++)
  16.             {
  17.                 colHd[i] = new System.Windows.Forms.ColumnHeader();
  18.                 colHd[i].Text ="item"+i.ToString("00");
  19.             }
  20.             this.lsvTEST.Columns.AddRange(colHd);
  21.             string[] item = new string[3];
  22.             for(int ii = 0;ii<20;ii++)
  23.             {
  24.                 item = new string[3]{"AA"+ii,"BB"+ii,"CC"+ii};
  25.                 this.lsvTEST.Items.Add( new System.Windows.Forms.ListViewItem(item) );
  26.             }
  27.             
  28.         }
  29.         
  30.         //キー動作
  31.         void LsvTESTKeyDown(object sender, KeyEventArgs e)
  32.         {
  33.             if (e.Control && e.KeyCode == Keys.C)
  34.             {
  35.                 var builder = new System.Text.StringBuilder();
  36.                 foreach(var selectItem in this.lsvTEST.SelectedItems)
  37.                 {
  38.                     ListViewItem item = selectItem as ListViewItem;
  39.                     if( item != null)
  40.                     {
  41.                         foreach(ListViewItem.ListViewSubItem itemSub in item.SubItems)
  42.                         {
  43.                             builder.Append(itemSub.Text);
  44.                             builder.Append("\t");
  45.                         }
  46.                         builder.AppendLine();
  47.                     }
  48.                 }
  49.                 Clipboard.SetText(builder.ToString());
  50.             }
  51.             
  52.         }

2019/02/09

ExcelDataReaderでxlsとxlsxとcsvを読み込み:powershellでも動くよ

C#でxlsとxlsxとcsvを読み込み。
using ExcelDataReader;しておく。
たぶんpowershellでも使えると思う。
出力はEPPLUS使う。




xlsは読み込みだけでいいと思う。
出力はxlsxで。
でも、元ファイルはxlsもあるのでExcelDataReaderを使う。
ExcelDataReaderのAsDataSet()は使わないのでreaderを回す。
GetInt32()はキャストしてエラーになるので、GetValue()をvarで受けて
tostring()でキャストする。

次の1行に移動して読む
Read() reads a row from the current sheet.

指定の列のセルを読む
GetValue() returns a value from the current row as an object, or null if there is no value.

次のシートに移動
NextResult() advances the cursor to the next sheet.

コラムの個数
FieldCount returns the number of columns in the current sheet.

行の個数
RowCount returns the number of rows in the current sheet. This includes terminal
empty rows which are otherwise excluded by AsDataSet().






ExcelDataReader
https://github.com/ExcelDataReader/ExcelDataReader

EPPLUS
https://github.com/JanKallman/EPPlus


ExcelDataReader.dllはここから。
https://www.nuget.org/packages/ExcelDataReader/
ExcelDataReaderのAsDataSet()使うならここからdll入手する。
でも、getValueでもいけるよ。
https://www.nuget.org/packages/ExcelDataReader.DataSet/


using (FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read))
   {

    using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream))
    {
     
     m_aryBaseDatMaxCol = reader.FieldCount;
     m_aryBaseDatMaxRow = reader.RowCount;
     
     if( reader.FieldCount < MAX_NUM_COL && reader.RowCount < MAX_NUM_ROW)
     {
      int i = 0;
//      do {
      while (reader.Read()) {
       for(int j = 0 ; j < reader.FieldCount; j++)
       {
        var val reader.GetValue(j)
        m_aryBaseDat[i,j] = val.Tostring();
       }
       i++;
      }
//      }  while (reader.NextResult());//advances the cursor to the next sheet.
     }
    }
syntax2html

powershellだとこんな感じ?
Set-ExecutionPolicy RemoteSigned -Scope Process
[Reflection.Assembly]::LoadFile("c:\tmp\ExcelDataReader.dll");

$path = "c:\tmp\test.xls";
$f = New-Object IO.FileStream($path, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read)

$reader =[ExcelDataReader.ExcelReaderFactory]::CreateReader($f);

$aryV = New-Object "object[,]" $reader.RowCount, $reader.FieldCount;
[int] $i=0;
while($reader.Read())
{
    for([int] $j=0;$j -lt $reader.FieldCount;$j++)
    {
    $aryV[$i,$j] = $reader.GetValue($j);
    }
    $i++;
}