/// <summary>>
/// Eason.Lin
/// 2024-01-04
/// 右键复制
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
if (dgv.GetCellCount(DataGridViewElementStates.Selected) > 0)
{
Clipboard.SetDataObject(dgv.GetClipboardContent()); //剪贴板
}
}
/// <summary>
/// Eason.Lin
/// 2024-01-05
/// 2024-01-10
/// Excel复制资料格式与工作表复制格式不一致
///
///数据范例1:工作表
///"A_2\tB_1\tC_1\tShift_1\tLine_1\r\n
///24.3574\t25.9452\t26.5994\tD\t1\r\n
///26.608\t23.7716\t25.2916\tD\t1\r\n
///27.672\t27.3187\t25.7836\tD\t1\r\n
///23.8072\t25.1559\t25.8289\tN\t1"
///
///数据范例2:Excel
/// "RESULT\tTIME\r\n
/// PASS\t2024-01-10 08:00:00\r\n
/// PASS\t2024-01-10 08:00:00\r\n
/// PASS\t2024-01-10 08:00:00\r\n
/// PASS\t2024-01-10 08:00:02\r\n
/// PASS\t2024-01-10 08:00:02\r\n
/// PASS\t2024-01-10 08:00:02\r\n
/// PASS\t2024-01-10 08:00:02\r\n"
///
///数据范例 3:csv
/// "SN\tLINE\r\n
/// \"\tA2225815443031\"\t\"\tC02\"\r\n
/// \"\tP717627610470\"\t\"\tP11\"\r\n
/// \"\tP717920641283\"\t\"\tP06\"\r\n
/// \"\tP717920641019\"\t\"\tP06\"\r\n
/// \"\tP717920640421\"\t\"\tP06\"\r\n
/// \"\tP717920640418\"\t\"\tP06\"\r\n
/// \"\tP717920640422\"\t\"\tP06\"\r\n
/// \"\tDM233846200183\"\t\"\tP01\"\r\n"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
#region 1.复制内容解析
string data = Clipboard.GetText();
List<string> dataList = data.Split(new string[] { "\r\n" }, StringSplitOptions.None).ToList();
List<List<string>> dataListList = new List<List<string>>();
for (int i = 0; i < dataList.Count(); i++)
{
List<string> tempList = new List<string>();
if (dataList[i].Contains("\""))
tempList = dataList[i].Replace("\"", "").Split(new string[] { "\t\t" }, StringSplitOptions.None).ToList(); //针对csv格式
else
tempList = dataList[i].Split(new string[] { "\t" }, StringSplitOptions.None).ToList(); //针对Excel格式
dataListList.Add(tempList);
}
#endregion
#region 2.鼠标选择粘贴区域--求第一个单元格
DataGridViewSelectedCellCollection dgvSC = dgv.SelectedCells;
int rowIndex = 0, colIndex = 0;
if (dgvSC.Count > 0)
{
rowIndex = dgvSC[0].RowIndex;
colIndex = dgvSC[0].ColumnIndex;
for (int i = 0; i < dgvSC.Count; i++) // rows * cols數量
{
//起始单元格位置,最前面,最上面
if (rowIndex > dgvSC[i].RowIndex)
rowIndex = dgvSC[i].RowIndex;
if (colIndex > dgvSC[i].ColumnIndex)
colIndex = dgvSC[i].ColumnIndex;
}
}
#endregion
#region 3.工作表区域拓展
DataTable dataTable = (dgv.DataSource as DataTable).Copy();
// 若大於現有表格的columns,新增columns
if (dataListList[0].Count() + 1 + colIndex > dataTable.Columns.Count - 1)
{
for (int i = dataTable.Columns.Count - 1; i < dataListList[0].Count() + colIndex - 1; i++)
dataTable.Columns.Add("C" + (i + 2));
}
//若大於現有表格的rows,新增rows
if (dataListList.Count() + 1 + rowIndex > dataTable.Rows.Count - 1)
{
for (int i = dataTable.Rows.Count - 1; i < dataListList.Count() + rowIndex; i++)
{
DataRow dr = dataTable.NewRow();
dataTable.Rows.Add(dr);
}
}
#endregion
#region 4.复制资料
for (int i = 0; i < dataListList.Count(); i++)
{
for (int j = 0; j < dataListList[i].Count(); j++)
{
#region 2023-03-27 Eason.Lin 百分数转double类型
if (dataListList[i][j].Contains("%"))
{
string sData = dataListList[i][j].Trim().Replace("%", "");
if (decimal.TryParse(sData, out decimal tryDecimal))
{
dataTable.Rows[rowIndex + i][colIndex + j] = (tryDecimal / 100).ToString();
}
else
dataTable.Rows[rowIndex + i][colIndex + j] = dataListList[i][j].Trim();
}
else
dataTable.Rows[rowIndex + i][colIndex + j] = dataListList[i][j].Trim();
#endregion
}
}
#endregion
dgv.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect; //可多選行 才不會崩潰
dgv.DataSource = dataTable;
dgv.CurrentCell = dgv.Rows[rowIndex].Cells[colIndex]; //光标停留在当前位置
}
catch (Exception)
{
throw;
}
}
C# 复制Excel和CSV资料至DataGridView
最新推荐文章于 2024-05-20 00:05:49 发布