如何删除excel中的所有行,除了c#中的头文件(How to delete all rows from excel except header in c#)
除了c#中的标题外,如何删除Excel电子表格中的所有行?
我试图使用
Microsoft.Office.Interop.Excel;
执行此操作Microsoft.Office.Interop.Excel;
图书馆我现在有了这个代码
Range xlRange = ws.UsedRange; int rows = xlRange.Rows.Count; Console.WriteLine(rows); for (int i = 2; i <= rows; i++) { ((Range)ws.Rows[i]).Delete(XlDeleteShiftDirection.xlShiftUp); }
但它并没有删除所有的行,我认为因为当它遇到某个行不再存在行时会删除行,我做错了什么?
我设法做到了,我从底部而不是顶部开始,所以现在我的循环是
for (int i = rows; i != 1; i--) { ((Range)ws.Rows[i]).Delete(XlDeleteShiftDirection.xlShiftUp); }
解
var range = (Range)ws.Range[ws.Cells[2, 1], ws.Cells[ws.UsedRange.Rows.Count, ws.UsedRange.Columns.Count]]; range.Delete(XlDeleteShiftDirection.xlShiftUp);
使用
get_Range
停止工作时,.Net的v4发生了变化How do I delete all rows in an excel spreadsheet except the header in c#?
I am trying to do this using the
Microsoft.Office.Interop.Excel;
libraryI now have this code
Range xlRange = ws.UsedRange; int rows = xlRange.Rows.Count; Console.WriteLine(rows); for (int i = 2; i <= rows; i++) { ((Range)ws.Rows[i]).Delete(XlDeleteShiftDirection.xlShiftUp); }
But it's not deleting all the rows, I think because it's deleting the rows when it hits a certain number that row is no longer there, what have I done wrong?
I managed to do it, I have started from the bottom instead of the top, so now my loop is
for (int i = rows; i != 1; i--) { ((Range)ws.Rows[i]).Delete(XlDeleteShiftDirection.xlShiftUp); }
Solution
var range = (Range)ws.Range[ws.Cells[2, 1], ws.Cells[ws.UsedRange.Rows.Count, ws.UsedRange.Columns.Count]]; range.Delete(XlDeleteShiftDirection.xlShiftUp);
There had been a change in v4 of .Net that using
get_Range
stopped working
原文:https://stackoverflow.com/questions/42647466
最满意答案
您应该在保存之前在用户上设置id属性:
$this->User->id = $id;
或者确保在
$this->request->data;
,你正在编辑的对象的id,$this->request->data['User']['id'];
在这种特殊情况下,请求数据上缺少用户ID会导致问题。You should set the id property on the user before you save it:
$this->User->id = $id;
Or make sure that in your
$this->request->data;
, the id of the object your are editing is present,$this->request->data['User']['id'];
in this particular case, the absence of the user id on the request data is causing the problem.
相关问答
更多-
OK ...我花了几个小时试图说明为什么userManager.updateAsync不会保留我们编辑的用户数据...直到我得出以下结论: 这个混乱是由于我们以如下一行创建了UserManager : var manager = new UserManager
(new UserStore (new MyDbContext())); 那么我们使用manager.UpdateAsync( user ); 但是这将在上下文中更新用户,然后我们将需 ... -
您应该在保存之前在用户上设置id属性: $this->User->id = $id; 或者确保在$this->request->data; ,你正在编辑的对象的id, $this->request->data['User']['id']; 在这种特殊情况下,请求数据上缺少用户ID会导致问题。 You should set the id property on the user before you save it: $this->User->id = $id; Or make sure that in ...
-
CakePHP 2.0 ACL - 更新用户记录会导致ARO错误(CakePHP 2.0 ACL - updating the user record results in ARO error)[2022-09-17]
根据评论,我只是把它写成答案。 我将用户模型设置为请求者,但实际上打算在相关的“组”模型上使用基于角色的身份验证。 我设置了父节点但忽略了从用户模型中取出Acts As Requester。 评论说它有一个魅力 - 我实际上并不需要为用户提供一个ARO对象(只是组),它只是因为Cake教程包含它。 Per the comments I'm just writing this up as the answer. I had the user model set up as a requester but ac ... -
ASP.NET EF6身份2更新用户声明错误更新用户(ASP.NET EF6 Identity 2 Update User Claims Error Updating For User)[2023-11-16]
我搞定了。 由于某种原因,appUser.Claims.Clear()无法正常工作,导致userId为null错误。 所以我在一个循环中手动删除旧的声明,现在一切正常。 [HttpPost, ValidateAntiForgeryToken] public async TaskClaims_UpdateByClaimGroup([Bind(Include = "Id,SelectedClaimGroupId")] UserClaimsViewModel viewModel) { ... -
你可以通过使用Session 。 喜欢: $user = ""; $loguser = $this->Session->read('Auth.User'); if(!empty($loguser)){ $user = $loguser['first_name']." ".$loguser['last_name']; } 更新(8/23/2015): 最近cakephp说SessionHelper已被弃用,你应该使用request->session()来代替。 $loguser = $this->r ...
-
django-rest-framework默认不更新嵌套的序列化程序。 要更新配置文件对象,您需要覆盖序列化程序的更新方法。 class UserSerializer(serializers.ModelSerializer): profile = ProfileSerializer() def update(self, instance, validated_data): ''' Override update method because we need ...
-
无法编辑cakephp中的数据(Unable to edit data in cakephp)[2024-03-29]
实际上我是从Android应用程序发送所有字段作为字符串。 在edit_profile函数中,我没有将它们转换回int,而验证它没有保存数据 Actually i was sending all of the fields from android app as a string. Here on edit_profile function i was not casting them back to int some while validation it did not save the data -
确保Auth的员工ID正常。 查询leave表,其中employee_is是登录用户 $employeeId = $this->Auth->user('id'); return $this->LeaveApplication->findAllByEmployeeId($employeeId); Make sure the employee ID from Auth is ok. Query the leave table, where the employee_is is the logged in us ...
-
如文档中所述 , Cloud.User.update 更新当前用户。 这可以解释为什么您的代码正在更新管理员用户。 但我也在用户文档中找到:更新当前用户以下参数 su_id :String [ ADMIN-ONLY ] 用户ID代表更新此用户。 当前登录用户必须是应用程序管理员才能代表其他用户更新用户。 我会尝试一下。 (顺便说一下,我从未使用它,但我希望它可以帮助你) As stated in the docs, Cloud.User.update Update the current user. Thi ...
-
创建/app/app_controller.php 。 在该控制器中创建beforeRender函数。 使用通常的$uses 。 看书 。 Create /app/app_controller.php. In that controller create the beforeRender function. Use the usual $uses. See the book.